How to traverse a DOM object in jQuery?
I have this HTML. How do I navigate to a tag <br>
using jQuery
?
Condition:
- do not use class attributes.
- the split method should not be used.
For example:
<html>
<body>
<div>
<div>
hello
</div>
123
<br>
paragraphs
<br>
escapes
<br>
</div>
</body>
</html>
For me the only option is to navigate using the tag <br>
so how can I go through <br>
and get the next data after <br>
.
I tried How to get HTML
it but couldn't loop with <br>
.
var html=$(body).html();
Result I need:
"Runs away"
Could you please help me get through the tag <br>
?
source to share
The texts you are looking for are not included in their own tags. They are called text nodes and jQuery has no easy way to capture them. But you can use a little crude JavaScript with your jquery to do this.
In this example, the script captures all text nodes in the outer-div ( contents()
captures every type of child node, then filter
on nodeType==3
only enough text nodes), and passes through them, alerting everyone:
$('body > div').contents().filter(function() {
return this.nodeType == 3;
})
.each(function(){
alert($(this).text());}
);
You will see that it finds white spaces inside the outer div before the paragraph and after the last line break (warning blank for the first and last text nodes), but also finds every piece of text between <br>
s
source to share
Try the following:
$('br').each(function(){
console.log($(this).val());
});
For more information see the following link: http://api.jquery.com/jquery.each/
Hope it helps.
source to share
You can visit
https://learnmayurinfosyssolutions.blogspot.com/2018/05/jquery-traversing.html
on this page everything about the passage.
source to share