Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery addClass to all Elements with Class

Tags:

jquery

find

each

I want to addClass() strike to class priceis if the class specialprice exists. Currently, it is only putting it on the first element, not on every element that appears on the page. I need it to check for all elements with the class specialprice.

<font class="pricecolor colors_productprice specialprice">
    <span class="PageText_L483n">
        <font class="text colors_text"><b>Regular Price:<br> </b></font>
        <span id="priceis">$2,492<sup>.38</sup></span>
    </span>
</font>

<script>
    $('.specialprice').find('.priceis').addClass('strike');
</script>
like image 331
ToddN Avatar asked May 02 '11 18:05

ToddN


3 Answers

maybe something like this? still not sure about the explanation of your question, not clear..

$(function() {
  $('.specialprice').each(function() {
    $(this).find('.priceis').addClass('strike');
  });
}

EDIT: just realized maybe you wanted it like this?

$('.specialprice').each(function() {
  $(this).find('*').addClass('strike');
});
like image 198
Alex K Avatar answered Sep 19 '22 15:09

Alex K


$('.specialprice.priceis').addClass('strike'); ?

like image 24
Jonas Schmid Avatar answered Sep 17 '22 15:09

Jonas Schmid


Your HTML is wrong

<span id="priceis">

You probably meant to set class instead of id.

<span class="priceis">

Your current code will work if you fix your HTML.

You might want to see this answer, it explains why a script doesn't work when included before the DOM elements.

like image 45
BrunoLM Avatar answered Sep 18 '22 15:09

BrunoLM