I'm trying to change id
and name
attributes on a page. The code I'm using is this:
var img = new RegExp( 'id*="launch_pad_image_slide_\d"', g ) ;
$('.slider-data').each(function(){
$(this).find(img).attr( 'id', 'random stuff' );
});
The assumption is the .find
function should pick up the whole 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 has to loop through them and number their id
/name
attributes properly to avoid doubles.
jQuery selector doesn't support regex syntax. However, you may use Attribute Starts With Selector:
$(this).find("[id^='launch_pad_image_slide_']").prop("id", "random stuff");
Another way is to filter
the elements:
$(this).find("[id]").filter(function() {
return /^launch_pad_image_slide_\d$/.test(this.id);
}).prop("id", "random stuff");
Try this :
$('img').each(function(){
if ($(this).attr('id').indexOf("launch_pad_image_slide_") >= 0)
$(this).attr('id',Math.random());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With