1

I would like to search for an image ID that contains a value AND a variable.

I need the below script to be something like $("[id*='MYVALUE'+x]") but that does not work.

for (x=0; x< imageList.length; x++) 
    {

    var imageSRC = imageList[x];

    //need this to be MYVALUE + the X val
    //search for IDs that contain MYVALUE + X
    $("[id*='MYVALUE']").attr('src', 'images/'+imageSRC);

};

<!-- These images are replaced with images from the array imageList-->
<img id="dkj958-MYVALUE0" src="images/imageplaceholder" />
<img id="mypage-MYVALUE1" src="images/imageplaceholder" />
<img id="onanotherpage-MYVALUE2-suffix" src="images/imageplaceholder" />

3 Answers 3

3

Take a look at this: http://jsfiddle.net/w2UUg/2/

$('[id*="MYVALUE' + x + '"]').attr('src', 'images/'+imageSRC);
Sign up to request clarification or add additional context in comments.

Comments

2
$("[id*='MYVALUE" + x + "']")

Should do what you want. Sorry this is really badly formatted, I'm attempting to type on a mobile.

1 Comment

Thanks, I would give you extra points for typing on a mobile. The quotes were backwards tho. Thanks!
0
$('img').filter(function () {
    return this.id.match(/myvalue\d+/i);
}).attr('src', 'images/' + imageSRC);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.