Reputation: 13
I have HTML like this. How to traverse to <br>
tag using jQuery
?
Condition:
For example:
<html>
<body>
<div>
<div>
hello
</div>
123
<br>
paragraphs
<br>
escapes
<br>
</div>
</body>
</html>
For me, only option is to traverse using <br>
tag,
so how can I loop through <br>
and get next data after <br>
.
I tried Like this getting the HTML
, but unable to loop through using <br>
.
var html=$(body).html();
Output I need:
"escapes"
Can you please help me to traverse through <br>
tag?
Upvotes: 0
Views: 215
Reputation: 15404
The texts you are seeking are not within their own tags. They are referred to as text-nodes, and jQuery doesn't have a simple way to grab them. But you can use a little raw JavaScript with your jquery to do so.
In this example the script grabs all of the text-nodes inside the outer-div (contents()
grabs every type of child node, then filter
on nodeType==3
grabs just the text-nodes), and loops through them, alerting each:
$('body > div').contents().filter(function() {
return this.nodeType == 3;
})
.each(function(){
alert($(this).text());}
);
You'll see that it finds the white-spaces just inside the outer-div before the paragraph and after the last line-break (the alert is empty for the first and last text-nodes) but also finds each piece of text between the <br>
s
Upvotes: 1
Reputation: 182
Try the following:
$('br').each(function(){
console.log($(this).val());
});
Check the following link for more info: http://api.jquery.com/jquery.each/
Hope it helps.
Upvotes: 0