Reputation: 16219
cshtml code
<div class="span3">
@for (var i = 0; i < Model.GroupNames.Count; i++)
{
<ul>
<li>
@{
var item = Model.GroupTypeNames[i];
var selected = i == 0 ? " class=\"selected\"" : "";
}
<a onclick="getGroupType(@item.Id);" data-option-value="*"@Html.Raw(selected)>@item.TypeName</a>
</li>
</ul>
}
</div>
jquery code
function getGroupType(id) {
debugger;
$.getJSON( "Groups/Get" + id, function (data) {
$(".span9").html($("#GroupTemplate").removeData());
$.each(data, function (index, value) {
if (value.Id != undefined) {
$("#GroupTemplate").tmpl(value).appendTo(".span9");
}
});
$('#circle').css("opacity", "0.5");
});
}
Unable to get call here
but when I try to use onclick="alert('Hello');"
it is working perfectly
what wrong with onclick="getGroupType(@item.Id);"
Upvotes: 0
Views: 911
Reputation: 74738
As you commented:
my function is in ready
so i think here is a scoping issue, because your onclick
handler is not in the ready function so your function getGroupType(@item.Id);
is not available for it, instead you can put it outside in the gloabal scope.
getGroupType(id){
// all the stuff
}
$(function(){
// all other stuff
});
Upvotes: 1
Reputation: 250902
I would expect you get the error:
ReferenceError: getGroupType is not defined
Because I suspect that your JavaScript code appears later in the file than your onclick
attribute.
When using attributes, make sure the function is defined before the attributes... usually in the <head>
. Having said this, normal practice is to include scripts later and attach the events in the script, rather than using attributes.
Also, make sure your function isn't wrapped inside another function. JavaScript is functionally scoped, and a function wrapped in another function won't be callable from global scope.
This all assumes id
is actually a number. If the id
is a string, remember to wrap it in quotes too. GUIDs must be treated like strings in JavaScript...
onclick="getGroupType('@item.Id');"
Upvotes: 1
Reputation: 7383
You will need to wrap the parameter with Single Quotes.
onclick="getGroupType('@item.Id');"
Upvotes: 0