javascript - Running if/each statement to select divs and alter background image position independently -
i trying change position of background image within div depending on text within span.
my problem multiple divs have same class not changeable.
for example. every cell want change image position looks like:
<td class="js-gridblock js-pre-order" data-attvalue1="3xl" data-attvalue2="plum"> <div class="js-gridimage"> <div class="prodpagegridtext" id="gridtext-gridbox34"> <span class="status">due in 4 weeks</span><br> <div class="prodpagegridtextarrow"> </div> </div> </div> </td>
where span class status value changes anywhere between 1-10.
my jquery looks like;
jquery(document).ready(function() { if (jquery('span.status:contains("4")')) { jquery('.js-pre-order .js-gridimage').css("background-position", "-4px -213px"); } if (jquery('span.status:contains("5")')) { jquery('.js-pre-order .js-gridimage').css("background-position", "-4px -242px"); } if (jquery('span.status:contains("10")')) { jquery('.js-pre-order .js-gridimage').css("background-position", "-4px -387px"); } });
my issue when jquery run every js-gridimage changes ("background-position", "-4px -387px")
need them change dependent on spanvalues.
i have seen .each() function unsure how best incorporate code.
thanks in advance.
you can use relationship between elements, here .closest(selector)
can used traverse fetch desired element , set css rules.
jquery(document).ready(function() { jquery('span.status:contains("4")').closest('.js-gridimage').css("background-position", "-4px -213px"); jquery('span.status:contains("5")').closest('.js-gridimage').css("background-position", "-4px -242px"); jquery('span.status:contains("10")').closest('.js-gridimage').css("background-position", "-4px -387px"); });
Comments
Post a Comment