Christian Isai
Christian Isai

Reputation: 146

Delete everything inside a tag but another tag jquery

I have this piece of html

    <div id="Cart">
    <p>
        You have <strong>1</strong> item in your cart.
        Your sub total is <strong>$35.00</strong>.
    </p>
    </div>

I need to be able to get the information between the <strongs> and delete everything else, is that possible with jquery? I tried $("#Cart strong:not()") But didn't select anything, I thought maybe placing the info in a variable, deleting everything with .remove and reprinting the variables but I don't really know how to do that, can someone help me?.

The information between the strong is dynamic, I believe all the other is in a php file that I dont have access to... maybe filtering the words and removing them? Any advice will be welcome :)

Upvotes: 2

Views: 215

Answers (3)

Evan
Evan

Reputation: 6115

OK so if I understand what you want, you want to get the information from the strongs.

if you are not averse to adding classes or ids to your strongs (I use id here), that is the best way.

so change ur html to:

<div id="Cart">
    <p>
        You have <strong id="numItems">1</strong> item in your cart.
        Your sub total is <strong id="cost">$35.00</strong>.
    </p>
</div>

and this is how you would get this in a string, which you can append or something where you need:

var yourString = "You have " + $('#Cart #numitems').text() + " item(s) in your cart. Your subtotal is: " + $('#Cart #cost').text();

Upvotes: 0

Roko C. Buljan
Roko C. Buljan

Reputation: 206078

$('#Cart p').contents(':not(strong)').remove();

DEMO

Upvotes: 1

deviousdodo
deviousdodo

Reputation: 9172

Here's some code to get you started:

var strongs = [];
$('#Cart strong').each(function() {
    strongs.push($(this).text());
});
$('#Cart').html('First strong contains: ' + strongs[0] + '; And the second: ' + strongs[1]);

Here's an example of it working: http://jsfiddle.net/SBRhk/

Upvotes: 3

Related Questions