351

I need to change an element's ID using jQuery.

Apparently these don't work:

jQuery(this).prev("li").attr("id")="newid"
jQuery(this).prev("li")="newid"

I found out that I can make it happen with the following code:

jQuery(this).prev("li")show(function() {
    this.id="newid";
});

But that doesn't seem right to me. There must be a better way, no? Also, in case there isn't, what other method can I use instead of show/hide or other effects? Obviously I don't want to show/hide or affect the element every time, just to change its ID.

(Yep, I'm a jQuery newbie.)

Edit
I can't use classes in this case, I must use IDs.

Pops
  • 30,199
  • 37
  • 136
  • 151
yoavf
  • 20,945
  • 9
  • 37
  • 38
  • 5
    In JavaScript, you can assign to a variable (`foo = 1`), a member of an object (`foo.bar = 2` or `foo['bar'] = 2`) or an array subscript (`foo[0] = 3`), but assigning to an expression, like the result of an operator (`(x + b) = 5`) or the result of a function call (`foo() = 1`, `foo(x).bar(y) = 7`) doesn't make any sense, so it's definitely not going to be the way things work in jQuery or any other JavaScript library. – rakslice Aug 19 '13 at 18:05

7 Answers7

587

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

jQuery(this).prev("li").attr("id","newId");
Eran Galperin
  • 86,251
  • 24
  • 115
  • 132
90

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)

Jeremy Moritz
  • 13,864
  • 7
  • 39
  • 43
47

What you mean to do is:

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

That will set the ID to the new ID

Pim Jager
  • 31,965
  • 17
  • 72
  • 98
11

I did something similar with this construct

$('li').each(function(){
  if(this.id){
    this.id = this.id+"something";
  }
});
Petr Safar
  • 107
  • 2
  • 11
7

I'm not sure what your goal is, but might it be better to use addClass instead? I mean an objects ID in my opinion should be static and specific to that object. If you are just trying to change it from showing on the page or something like that I would put those details in a class and then add it to the object rather then trying to change it's ID. Again, I'm saying that without understand your underlining goal.

Tim Knight
  • 8,346
  • 4
  • 33
  • 32
  • 3
    the joys of legacy systems - I understand your pain then. Eran answer is definitely the best then. – Tim Knight Dec 07 '08 at 19:44
  • 7
    Actually setting the id is a useful and worthwhile thing to do if you're building up the DOM by using hidden-templates in the HTML and then cloning them using jquery. – Steve Knight Aug 02 '11 at 08:40
4
<script>
       $(document).ready(function () {
           $('select').attr("id", "newId"); //direct descendant of a
       });
</script>

This could do for all purpose. Just add before your body closing tag and don't for get to add Jquery.min.js

0

Eran's answer is good, but I would append to that. You need to watch any interactivity that is not inline to the object (that is, if an onclick event calls a function, it still will), but if there is some javascript or jQuery event handling attached to that ID, it will be basically abandoned:

$("#myId").on("click", function() {});

If the ID is now changed to #myID123, the function attached above will no longer function correctly from my experience.

rfornal
  • 5,072
  • 5
  • 30
  • 42