2

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>

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

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!!
2
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.

3 Comments

I thin you forgot a ' after minimal-price-link
This script would add a "strikethrough" class to ALL "regular-price" spans if but one "minimal-price-link" existed.
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.
1
if ($("a.minimal-price-link").length > 0)
{
    $("span.regular-price").addClass("yourclass");
}

<style type="text/css">
    .yourclass { text-decoration: line-through; }
</style>

1 Comment

I think this code would add "yourclass" to ALL the regular-price spans, if one "minimal-price-link" class exists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.