Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JQuery to change element ID

I've been trying to change an element ID using jquery for an A/B test.

From what ive seen, it should go something like this:

$('#tag').attr('oldId','newId');

But so far, no dice - any ideas?

*I've looked through the responses and i think i had the wrong end of the stick! I was assuming the current / old id had to be placed where ive labelled 'oldId', rather than id, followed by the replacement - silly mistake, thanks for all the help :)

like image 400
Rob Avatar asked Feb 28 '26 23:02

Rob


2 Answers

Try to use the proper signature of .attr('attributeName','value'),

$('#tag').attr("id","newId")
like image 199
Rajaprabhu Aravindasamy Avatar answered Mar 02 '26 14:03

Rajaprabhu Aravindasamy


Edited The right syntax is: $("#myId").attr("id", "mySecondId")

like image 37
naoxink Avatar answered Mar 02 '26 12:03

naoxink



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!