I’m retrieving/fetching content with jQuery AJAX, displaying it on the page and trying to get text from the following span with 3 class names:
<span class="price eur priceData">198,91 €</span>
How can I get the number “198,91” from that span?
I’m using the following code to retrieve the text (which isn’t working):
$("span.price.eur.priceData").text();
You’ve requested more code, here it is:
$(document).ready(function() {
Read();
});
function Read() {
$.ajax({
url: "index.php?action=getData",
cache: false,
success: function(html){
if(html == "BAD") {
$("#my_results").empty().append("Failed!");
} else {
$("#my_results").empty().append("Successful!");
$("#page_content").empty().append('<xmp>'+html+'</xmp>');
var text=$("span.price.eur.priceData").text();
alert(text);
}
}
});
}
I’ve even tried to use SetTimeout() and Delay() and still didn’t work.
Solution: (thanks to Shadow Wizard)
$(html).find("span.price.eur.priceData").text();
Your code fails because you wrap the HTML contents with
<xmp>tags, rendering them as pure text thus jQuery can’t find any elements in there.You can use the raw contents like this: