Robert
Robert

Reputation: 29

How to get value of a tag?

I have the following html on my asp.net page and I would like to get just the value within the p tag using jquery.

<p><pi>€ </pi>213,47</p>
           

I tried the following

var Product = {};
Product.Price = $(this).closest(".caption").find("p:eq(0)").html();

But this gives me "<pi>€ </pi>213,47" but I want to get just the value 213,47.

Upvotes: 1

Views: 63

Answers (4)

Ahmed Shamel
Ahmed Shamel

Reputation: 1962

Try this:

var Product = {};
Product.Price = $(this).closest(".caption").find("p:eq(0)").text().replace('€','');

Update

Since jQuery 3.4 :eq selector is not supported, so it should not used be anymore. As an alternative you can use .eq(0) instead of :eq(0):

var Product = {};
Product.Price = $(this).closest(".caption").find("p").eq(0).text().replace('€','');

Upvotes: 1

MajiD
MajiD

Reputation: 2585

You can do this :

var Product = {};
var caption = $(".caption"); // change this with your selector
Product.Price = caption.find("p").eq(0).text();
console.log(Product)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="caption">
  <p>
    <pi>€ </pi>213,47</p>
</div>

Upvotes: 1

pavel
pavel

Reputation: 27102

There are many ways, for example...

var p = $('p');
var price = p.html().replace(/<pi>[^<]*<\/pi>/, '');

https://jsfiddle.net/0hLxje8z/

Upvotes: 1

Alessio Cantarella
Alessio Cantarella

Reputation: 5211

You could get the text of pi and remove it from p, i.e.

let pi = $('p > pi').text();
let p = $('p').text();
console.log(p.replace(pi, ''));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p><pi>€ </pi>213,47</p>

Upvotes: 1

Related Questions