Muhammad Usman
Muhammad Usman

Reputation: 10936

add id dynamically to jquery tabs

I am creating jquery tabs dynamically. I want to associate the the id to each tab.

$(function() {
    var index = 0;
    $("#addTab").live('click', function() {
        index++;
        var title = 'Tab.....  ' + index;
        var url = '#fragment-' + index;

        addTab(url, title, index);
        $('li.ui-state-default').attr("id",index);
});

This code successfully assigns the id. But when I create a new tab. It assigns the id to whole class. I didn't want to do this. I just want to assign the unique id to each class

JS Fiddle

http://jsfiddle.net/gP3YZ/9/

Upvotes: 1

Views: 11816

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87073

$('li.ui-state-default:last').attr("id",index).attr('id');

DEMO

Upvotes: 4

Engineer
Engineer

Reputation: 48793

$('li.ui-state-default').each( function(){
    $(this).attr("id",index++);
});

By the way, it's bad practice to use numbers as DOM Element's id, use something like ("item-"+index) as id attribute.

Upvotes: 4

Related Questions