Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate all elements and change their id

Tags:

html

jquery

jsp

I have a .jsp page where many elements have ids that end with a certain string. For example:

<div id="topActions-1083"></div>
<div id="collapse-1083">
<input id="collapse1Input-1083" type="hidden" value="expanded"></input>
</div>

Which is the fastest way to get all elements with id ending in '1083' and change it to '1084' ?

like image 398
Theodore K. Avatar asked Jun 19 '14 11:06

Theodore K.


Video Answer


1 Answers

Try this

$("[id*=1083]").each(function(){
   var iid = $(this).attr('id')
   var fin = iid.replace('1083','1084')
   $(this).attr('id',fin)
   console.log(fin)
});

Working DEMO

Selectors Example :

Starts with a given string (for example 1083),

$("[id^='1083']")

If you want to select elements which id contains a given string :

$("[id*='1083']")

If you want to select elements which id is not a given string :

$("[id!='1083']")
like image 172
Sridhar R Avatar answered Oct 10 '22 05:10

Sridhar R