Add and remove ID like add class in JQuery

21,233

Solution 1

$('#calendar-links a').click(function(e){
    $("#myElement").attr("id", "newId");
});

Where newId is the "new" ID for your element.

Solution 2

The ID can be set like the following:

 $(this).attr('id','newID');
Share:
21,233
user3029697
Author by

user3029697

Updated on July 09, 2022

Comments

  • user3029697
    user3029697 almost 2 years

    I have the folowing code that switches out a class on a div. I need something similar but with id. Is that done with get attribute? Not sure how this should be done but it should work the same as the code below in that a clicked link changes the ID..

    $('#calendar-links a').click(function(e){
        e.preventDefault();
        var calendar_class = $(this).attr('data-class');
        $('#calendar-links a').removeClass('active');
        $(this).addClass('active');
        $('#aecp-calendar').removeClass('all current future events').addClass(calendar_class);
    });