Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment div id value?

I have got a task to set the menu as selected. For that I use dynamic id. So I want to increment it with respect to the selection

My code is

<div class="menuHeader ui-corner-top" id="menu"+ i>
    <span><a href="#" onclick="Home()">Home</a></span>
</div>
<div class="menuHeader ui-corner-top" id="menu"+ i>
    <span><a href="#" onclick="NewTransaction()">New Transaction</a></span>
</div>
<div class="menuHeader ui-corner-top" id="menu"+ i>
    <span><a href="#" onclick="Portfolio()">Portfolio</a></span>
</div>

javascript is

$(document).ready(function () {
             alert(document.URL);
             var list = $("#menu");
             for (var i = 1; i <= list.length; i++) {
                 list[i].innerHTML = i;
             }
             var str = document.URL.toLowerCase().indexOf("portfolio/index");
             alert(str);
             if (str >= 0) {
                 $('#menu').addClass("menuHeaderActive");
             }
         });

How can I do this?

like image 613
Nithin Viswanathan Avatar asked Apr 01 '13 14:04

Nithin Viswanathan


2 Answers

var i=0;
$('.menuHeader').each(function(){
    i++;
    var newID='menu'+i;
    $(this).attr('id',newID);
    $(this).val(i);
});
like image 125
writeToBhuwan Avatar answered Nov 07 '22 19:11

writeToBhuwan


This is the way to do it with Jquery

val elementList = $(".menu");
for (var i = 1; i <= list.length; i++) {
    elementList[i].attr("id", "menu" + i);
}
like image 1
Gumbo Avatar answered Nov 07 '22 18:11

Gumbo