Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex with .find(), no results

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.

like image 352
Orangeman555 Avatar asked Dec 19 '12 11:12

Orangeman555


2 Answers

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");
like image 148
VisioN Avatar answered Oct 07 '22 01:10

VisioN


Try this :

    $('img').each(function(){
           if ($(this).attr('id').indexOf("launch_pad_image_slide_") >= 0)
               $(this).attr('id',Math.random());
    });
like image 22
hsuk Avatar answered Oct 07 '22 01:10

hsuk