0

I have the following HTML code on my page. There is no containing element, it's just in the body.

<b>SqFt per Carton: </b>24.30<br>

Using script, I want to wrap 24.30 in a span tag with a class so the result will look like this:

<b>SqFt per Carton: </b><span class="sqft_cart">24.30</span><br>

How can I do this?

3
  • 1
    put the html code properly put it on a new line afer 4 spaces. check meta Commented Dec 28, 2011 at 7:12
  • i had posted the html code but the code is not shown. Commented Dec 28, 2011 at 7:19
  • following is the code:<b>SqFt per Carton: </b> 24.30 <br> i want it like the following:<b>SqFt per Carton: </b> <span class="sqft_cart">24.30</span> <br> Commented Dec 28, 2011 at 7:20

4 Answers 4

1

Here is jQuery way to achieve what you asked for by iterating over all text nodes (i.e. text without any tag) in the document, and in case they come right after <b> tag replace them with <span> having proper class:

$(document).ready(function() {
    $("body").contents().filter(textNodeFilter).each(function(index) {
        var textNode = $(this);
        if (this.previousSibling && this.previousSibling.tagName.toLowerCase() === "b") {
            var value = textNode.text();
            var oSpan = $("<span>").html(value).addClass("sqft_cart");
            textNode.replaceWith(oSpan);
        }
    });
});

function textNodeFilter() {
    return this.nodeType == 3;
}

Live test case.

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

2 Comments

but there are many <b> tags in the page so it will replace for all right?
Yes it will replace all "pure text" that come right after <b>.
1
$("b").parent().contents().filter(function() {
    return this.nodeType != 1;
}).wrap("<span class='sqft_cart'></span>");

http://jsfiddle.net/FW8Ct/4/

Comments

0

Since you don't have a containing element you can use .nextSibling() to get the text node for 24.30. Then .insertAdjacentHTML() inserts the new span after deleting the old text node. Since I don't know what the rest of your page looks like, I'll assume there could be multiple <b> elements, but the code will work either way.

Demo: http://jsfiddle.net/ThinkingStiff/6ArzV/

Script:

var bs = document.getElementsByTagName( 'b' );

for( var index = 0; index < bs.length; index++ ) {

    if( bs[index].innerHTML.indexOf( 'SqFt per Carton:' ) > -1 ) {
        var text = bs[index].nextSibling,
            span = '<span class="sqft_cart">' + text.nodeValue + '</span>';
        text.parentNode.removeChild( text );
        bs[index].insertAdjacentHTML('afterEnd', span);
    };

};

HTML:

<b>SqFt per Carton: </b>24.30<br>
<b>Price per Carton: </b>193.00<br>
<b>SqFt per Carton: </b>12.90<br>
<b>Price per Carton: </b>147.00<br>
<b>SqFt per Carton: </b>14<br>

CSS:

.sqft_cart {
    color: red;
}

Output:

enter image description here

Comments

0

you could get the value of SqFt per Carton: 24.30 by using

var takentext = $("class Or ID of SqFt per Carton").text();
$("class of span").text(takentext);

Comments

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.