Getting text from a paragraph in jquery

It can be very simple in jquery, but I can't 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 that doesn't seem to work for me. I can get the paragraph object, but not the text. I tested it using console.log without using.

+5
source share
4 answers

What you should work on ( you can test it here ), make sure you run it when the DOM is ready, though, for example:

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

If it works earlier, the elements may not be ready, and your selector will not find matches.

class body, ".body" "body" ( <body>). , .class:

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

.html() html .

$('.body a p').html();

0

, , . " HTML-" W3C

, . , . , " " , .

a , p, .

0
source

here is your paragraph element in html or php file which is id id tt

<p id="tt">ALert Message </p>
Run code
in the jquery file, to get the text of your paragraph with tt id, you can

var tt = $("#tt").text();
alert(tt);
Run code

works fine for jquery-3.1.1.js

0
source

All Articles