I want to create a couple if statements grabbing the ID using contains (*=) and img that equals (=). What might I be doing wrong?
if($("div[id*='Point_'], [img='gray.png']").length > 0 ) {
// do something gray
}
if($("div[id*='Point_'], [img='red.png']").length > 0 ) {
// do something red
}
<div id="Point_1">
<img src="gray.png">
</div>
<div id="Point_2">
<img src="red.png">
</div>
You’re attempting to use a tag selector like an attribute selector. If you want to match the
<img>tag where thesrcattribute equalsgray.pngthen the correct selector is$('img[src="gray.png"]').Also, this may or may not be correct for what you want to do, but the comma in a jQuery selector acts like an OR, so you’re saying “Give me any
<div>element where theidattribute contains the text ‘Point_’ OR any<img>element where the src matches ‘gray.png’.” If that’s what you want, then ok.However, it seems more likely that what you want to do is select the
<img>element with asrcequal togray.pngthat is within a<div>with an ID containing ‘Point_’, so the selector should be: