views:

255

answers:

3

I don't know much about jquery at all, so bear with my ignorance, but I'm pretty sure I can accomplish this with jquery.

I need jquery to check if an element exists, and if so, add a class to a different element. For example,

if class "minimal-price-link" exists, then add a class to "regular-price" or what I really am wanting to do is make the regular-price strikethrough.

Here's the code:

<div class="price-box">
   <span class="regular-price" id="product-price-2">
       <span class="price">$240.00</span>
   </span>               

   <a href="http://stemulation.com/order/sfs30.html" class="minimal-price-link">
       <span class="label">Your Price:</span>
       <span class="price" id="product-minimal-price-2">$110.00</span>
   </a>
</div>
A: 
if($('.minimal-price-link').length != 0)
{
  $('.regular-price').addClass('strikethrough');
}

If any element has the minimal-price-link class, add the strikethrough class to all elements with the regular-price class. This may not be exactly what you want, but it should give you the idea.

Matthew Flaschen
I thin you forgot a ' after `minimal-price-link`
webdestroya
This script would add a "strikethrough" class to ALL "regular-price" spans if but one "minimal-price-link" existed.
fudgey
Awesome guys! Thanks for the help...it works perfectly. I really like the demo from Reigel. I'm going to have to learn this jquery thing once and for all. Keep up the good work.
Buddy
A: 
if ($("a.minimal-price-link").length > 0)
{
    $("span.regular-price").addClass("yourclass");
}

<style type="text/css">
    .yourclass { text-decoration: line-through; }
</style>
rahul
I think this code would add "yourclass" to ALL the regular-price spans, if one "minimal-price-link" class exists.
fudgey
+2  A: 

this should do it...

$(function() {

  $("a.minimal-price-link").each(function(){

    // it would be easy to wrap it with strike
    $(this).closest('.price-box').find("span.regular-price").wrap("<s>");

    // or you can also add a class
    // $(this).closest('.price-box').find("span.regular-price").addClass("strike");
  })

});​

demo

Reigel
Thanks Reigel. It works perfectly. Adds a strikethrough only to those products that have a special price. The products without the special price are styled normally like they should. Perfect!!
Buddy