I want to set the menu to active when click on it. This is what I’ve tried :
<script language="javascript" type="text/javascript">
var select;
$(document).ready(function () {
$.getJSON(url, function (data) {
$.each(data, function (index, dataOption) {
var new_li = $("<li class='level1' id='select_list'><a href='javascript:void(0);' id='" + dataOption.ID + "' class ='selectedcategory'>" + dataOption.Name + "</a>");
$('a#' + dataOption.ID).click(function () {
select = "selected";
$('.level1').attr("id",select);
});
});
});
});
</script>
What I tried to do is to set the id of ‘level1’ to selected, when I click on that link. But my coding is set all the link to selected, even I click only 1 link.
Could anyone give me the solution please.
Thanks so much.
That happens because you are calling
.attr()on all.level1element instead of only the one being clicked by its child so,change
to
You also need to remove the “selected” id from the other
<li>‘s bySo the complete code looks like:
One suggestion though, class is usually used in situation like this when you want to have a
selectedtype of a list of menu, if you are using class you’d do something likeLooks more concise that way, but I’ll leave it up to you 🙂