Regex with .find (), no results

I am trying to change the id and name attributes on the page. The code I use is as follows:

 var img = new RegExp( 'id*="launch_pad_image_slide_\d"', g ) ; $('.slider-data').each(function(){ $(this).find(img).attr( 'id', 'random stuff' ); }); 

The .find function is .find to take the entire id inside:

 id="launch_pad_image_slide_2" 

... but it does not work.

5 hours on this and burned out. Suggestions? Basically, every time a field is deleted, jQuery should scroll through them and assign id / name attributes correctly to avoid doubling.

+7
source share
2 answers

The jQuery selector does not support regex syntax. However, you can use the attribute begins with a selector :

 $(this).find("[id^='launch_pad_image_slide_']").prop("id", "random stuff"); 

Another way is filter elements:

 $(this).find("[id]").filter(function() { return /^launch_pad_image_slide_\d$/.test(this.id); }).prop("id", "random stuff"); 
+10
source

Try the following:

  $('img').each(function(){ if ($(this).attr('id').indexOf("launch_pad_image_slide_") >= 0) $(this).attr('id',Math.random()); }); 
+1
source

All Articles