Categories
JavaScript Answers

How to set a new ID for a element with jQuery?

Spread the love

Sometimes, we want to set a new ID for a element with jQuery.

In this article, we’ll look at how to set a new ID for a element with jQuery.

How to set a new ID for a element with jQuery?

To set a new ID for a element with jQuery, we can call the attr method.

For instance, we write:

<div>
  hello world
</div>

to add a div.

Then we write:

$('div').attr('id', 'foo')

to select the div with $.

Then we call attr with the attribute name and value respectively.

Therefore, the div should now have the id attribute set to foo.

Conclusion

To set a new ID for a element with jQuery, we can call the attr method.

Leave a Reply

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