jQuery – Add ID instead of Class

jQuery – Add ID instead of Class

Try this:

$(element).attr(id, value);

So it becomes;

$(function() {
    $(span .breadcrumb).each(function(){
        $(#nav).attr(id, $(this).text());
        $(#container).attr(id, $(this).text());
        $(.stretch_footer).attr(id, $(this).text())
        $(#footer).attr(id, $(this).text());
    });
});

So you are changing/overwriting the id of three elements and adding an id to one element.
You can modify as per you needs…

Keep in mind this overwrites any ID that the element already has:

 $(.element).attr(id,SomeID);

The reason why addClass exists is because an element can have multiple classes, so you wouldnt want to necessarily overwrite the classes already set. But with most attributes, there is only one value allowed at any given time.

jQuery – Add ID instead of Class

$(selector).attr( id, yourId );

Leave a Reply

Your email address will not be published. Required fields are marked *