JqueryNo Comments

default thumbnail

Like toggleSlide or toggleClass, we can toggle the attribute in jquery easily. If you want to add or remove any attribute in html element, try below code.

HTML

<ul>
    <li id="item1">Home</li>
    <li>About</li>
</ul>

Jquery

If you want to add data-target attribute in first item of ul

$("#item1").click(function(){
    $('#item1').attr('data-target', function(index, attr){
        return attr == '2' ? null : '2';
    });
});

Be the first to post a comment.

Add a comment