How can I go through this DOM using jQuery and get the required text?

I have one form on the page that contains a bunch of related tags. I would like to get text between each h5 tag using jQuery. I would rather have a callback or do it in a simple loop construct where I can capture the text and make it rich in HTML, and then paste the end line in another place.

How to do it using jQuery?

+4
source share
2 answers

This will capture all h5 in form and alert them to text. You can do everything from there.

$('#myform h5').each(function() { alert($(this).text()); }); 
+6
source

By sibling tags, you mean that they are at the same level as the form, for example:

 <form id="the-form"> ... </form> <h5>Title 1</h5> <h5>Title 2</h5> <h5>Title 3</h5> 

If this is correct, you can do something like:

 $("#the-form ~ h5").each(function (){ // do something with $(this).text() }); 

Pay attention to the ~ symbol, which selects brothers and sisters.

Or, if you prefer to send text to the callback:

 function callback( textFound ){ // do something with textFound } $("#the-form ~ h5").each(function (){ callback( $(this).text() ); }); 
0
source

All Articles