ToddN
ToddN

Reputation: 2961

jQuery addClass or wrap in unlikely part of element

I really hate the way my shopping cart is set up as I CANNOT edit much of the pages. So I resort to using jQuery which is both fun and frustrating because we have to use so much of it. That being said..I am trying to find a way through jQuery to add a class specifically for the price given two situations. Unfortunately the price is not set up to add a class to it specifically (why my shopping cart did it this way is beyond me!). Any help would be appreciated!

UPDATE: Perhaps I need to use wrap() method to wrap the price and give it a class?

<-- Situation 1 -->
<b>
  <font class="text colors_text">
  <b>Regular Price: </b>
  </font>
   $2,533.31
</b> 

<-- Situation 2 -->
<b>
  <span class="exclusive">Exclusive Price:</span>
  <font class="pricecolor colors_productprice">$2,343.30</font>
</b>

I'm not going to dwell into the reasons why I need to add a class to these prices because there is a lot riding behind the class name which enables much of the page to render correctly. The class names for each situation should be the SAME. Situation 2 I may be able to handle with ease $('.exclusive').next('font').addClass('priceis') but Situation 1 is pretty tough.

Upvotes: 2

Views: 268

Answers (2)

LiverpoolsNumber9
LiverpoolsNumber9

Reputation: 2394

// get first "b" element
var b = $("b:eq(0)");

// get font element
var font = b.find("font:eq(0)");

// clone font element
var fontClone = font.clone();

// remove font element
font.remove();

// get remaining text (the price)
var price = b.html();

// replace "b" tag's html with span with class "priceis" containing price
b.html("<span class=\"priceis\">" + price + "</span>");

// put the font clone back in
b.preprend(fontClone);

Upvotes: 1

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

For situation one you could do:

var font = $(".text");//get the font tag
$(font.get(0).nextSibling).wrap('<span class="priceis" />'); 
//wrap it's next sibling inside a span

this add a span around the price with the desired class

Upvotes: 3

Related Questions