0

I have a huge page with a lot of text not specifically tagged within a text tag (h3, h1, p, whatever). So, stuff like this:

<div class="class">Here's some text that really should be in a p tag, but oh well</div>

I would like to be able to select all this text at once using jQuery, but I don't know how to select the text that isn't in a text-specific tag. Is there some implied tag or something, so that I can go like $('text') and call it a day?

Sasha
  • 6,224
  • 10
  • 55
  • 102
  • possible duplicate of [jQuery get the text of all elements in a page](http://stackoverflow.com/questions/9533860/jquery-get-the-text-of-all-elements-in-a-page) – Tchoupi Aug 22 '13 at 19:54
  • You want to select part of content of the div or the whole thing? – jcubic Aug 22 '13 at 19:55
  • I think I didn't ask that clearly. I was essentially looking for a way to just grab all the tags surrounding text. ie, something like $('h1, p, h2, h3, h4, a'). But I hadn't heard of the text method, so thanks, all! I think my solution is just to go through the page and painstakingly list all the specific div classes (cause doing just $('div') takes a while to load. – Sasha Aug 23 '13 at 04:46

5 Answers5

1

You can grab the element by the class/element selector even if it's not an element that's specific for containing text.

$('.class').text();

$('div').text();
Christopher Marshall
  • 10,678
  • 10
  • 55
  • 94
1

You can grab the 'text' within the div generically, regardless of class, if this is your wish. In this case, I am affecting the text with CSS.

$('div').text(function(){
  $(this).css('color', 'blue');
});

Codepen link

kunalbhat
  • 1,709
  • 10
  • 11
  • That makes no sense? Just doing `$('div').css('color', 'blue');` would do the exact same thing? – adeneo Aug 22 '13 at 20:02
  • Sure. It was just an example. OP could do multiple things within the function, but yes you're right, you could just do `.css()` :), perhaps not the BEST example. Main point is the `.text()` – kunalbhat Aug 22 '13 at 20:05
1

as far as I understand you want to get all the text in the page, but it is stored into different tags with different id's, so going trough all the tags and ids will be a large task.

Simply get all the text like this:

var t = $("body").text();

and it will get the text in the body of the document regardless the elements types or ids.

See this fiddle for reference: http://jsfiddle.net/7Xsxz/

RicardoE
  • 1,665
  • 6
  • 24
  • 42
1

With JavaScript you would need to find the element and grab the innerText:

var el = document.getElementsByClassName("class")[0];
var text = el.innerText;

With jQuery you can use the .text() method:

var text = $("div.class").text();

EXAMPLE

Chase
  • 29,019
  • 1
  • 49
  • 48
1

u need to get only the text provided in the tags where it should not be there in that case $('body').text() or $('div').text() both will give u false result, as they will give u all the text in it whatever the tags maybe of the child element.

u should code something like this:

var arr = new Array('P','SPAN','SCRIPT');
$(document).ready(function(){
  getText($('body'));
});

function getText(obj){
  obj.children().each(function(){
    if(arr.indexOf($(this).prop('tagName'))==-1)
      alert($(this).clone().children().remove().end().text());
      getText($(this));
  });
}

working example