Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  145] [ 6]  / answers: 1 / hits: 35936  / 14 Years ago, tue, august 24, 2010, 12:00:00

This might be a very simple thing in jquery but I am not able to figure it out. My html document has the following structure



 <div class=body> 
<a href=/question?id=70><p>This is the text I want to extract</p></a>
</div>


I tried this



$(body).find(a p).text()


but this does not seem to be working for me. I am able to get the paragraph object but not the text. I tested it with console.log with no use.


More From » jquery

 Answers
1

What you have should be working (you can test it here), make sure you're running it when the DOM is ready though, like this:



$(function() {
alert($(body).find(a p).text()); //or just $(a p).text()
});


If it runs earlier, the elements may not be ready, and your selector won't find any matches.



If you want to select the class body make sure to use .body instead of body (which would select the <body> element). Here's a version using the .class selector:



$(function() {
alert($(.body a p).text());
});

[#95835] Friday, August 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tiffany

Total Points: 46
Total Questions: 97
Total Answers: 84

Location: Comoros
Member since Tue, Mar 14, 2023
1 Year ago
;