user5521719
user5521719

Reputation: 1

Accessing div img id using jquery and replacing it

I have a problem finding img src inside a div. With a function l find all the divs with specific ids

function computerdiv(){
    computerarray = $("div[id^='your']");
    computerarray = _.shuffle(computerarray);
}

This is what is returned

<div id="your5">
     <img id="background" src="icon.png">
     OR
     <img id="background" src="icon2.png">
</div>

Depending on the div.

Then with another function l take the first item of the array and look if it contains icon or icon2

function computerturn(){ 
       chosen = computerarray[0];
       computerchoose = this.chosen;
       computerarray.splice(0,1);
       if("#computerchoose img[src=='icon2.png']"){
           $(computerchoose).find('img').remove();
           $(computerchoose).prepend('<img id="background" src="newimage.png" />');
       }else{
            $(computerchoose).find('img').remove();
            $(computerchoose).prepend('<img id="background" src="boom.png" />');
       }
}

If the div contains icon2 then it should be replaced if not then it should be replaced with another image. But l can't get it to work properly

Upvotes: 0

Views: 38

Answers (1)

tymeJV
tymeJV

Reputation: 104775

Dont use the == inside the selector - also, you're missing the $ in your selector

if ($("#computerchoose img[src='icon2.png']").length) {

Upvotes: 1

Related Questions