user1955162 user1955162 - 5 months ago 9
jQuery Question

Pass name of function to execute in parameter using jQuery

I have a function that is being called when a tab is clicked in HTML. I want to pass a function name unique to each tab in the

onClick()
action of that tab, and the specific function populates the HTML content of the tab space. My code thus far:

When I try to execute it, I get :


selectedFunction is not a function


And thats right!
selectedFunction
is not a function, its a variable containing the name of the function. How do I call the function who's name is contained in that variable? I'm testing with warehouse_availability.

See here:

<html>
<head>
<script>
function form_selectTab(selectedTab, selectedFunction, item_id){
$('.form_tab').css('background-color', 'rgb(222,222,222)');
$(selectedTab).css('background-color', 'white');
$('#form_tabSpace').html(selectedFunction(item_id));
}
function warehouse_availability(item_id){
return('This is warehouse availability for ' + item_id);
}
</script>
</head>
<body>
<div>
<table style="width: 100%; border-collapse: collapse;" cellspacing="0" cellpadding="0">
<tr>
<td class=form_tab onclick="form_selectTab(this, 'warehouse_availability', 'Items Name');">
Warehouse availability
</td>
<td class=form_tab onclick="form_selectTab(this, 'manufacturer_data', 'Items Name');">
Manufacturer data
</td>
<td class=form_tab onclick="form_selectTab(this, 'replacement_skus', 'Items Name');">
Replacement SKUs
</td>
<td class=form_tab onclick="form_selectTab(this, 'similar_items_in_stock', 'Items Name');">
Similar items in stock
</td>
<td class=form_tab onclick="form_selectTab(this, 'pricing', 'Items Name');">
Pricing
</td>
<td class=form_tab onclick="form_selectTab(this, 'special_quotes', 'Items Name');">
Special Quotes
</td>
</tr>
<tr>
<td colspan=6 id="form_tabSpace">
</td>
</table>
</div>
</body>
</html>

Answer

Function names are variables, not strings, they shouldn't be put in quotes.

        <td class=form_tab onclick="form_selectTab(this, warehouse_availability, 'Items Name');">
Comments