Changing an element's ID with jQuery

Solution 1:

Your syntax is incorrect, you should pass the value as the second parameter:

jQuery(this).prev("li").attr("id","newId");

Solution 2:

A PREFERRED OPTION over .attr is to use .prop like so:

$(this).prev('li').prop('id', 'newId');

.attr retrieves the element's attribute whereas .prop retrieves the property that the attribute references (i.e. what you're actually intending to modify)

Solution 3:

What you mean to do is:

jQuery(this).prev("li").attr("id", "newID");

That will set the ID to the new ID

Solution 4:

I did something similar with this construct

$('li').each(function(){
  if(this.id){
    this.id = this.id+"something";
  }
});