Below is the structure of my HTML, generated within D365 Portal.
How can I store the info of the div
after the paragraph tag with id="b"
?
<div class="hidden">
<p id="a">66 message</p>
<p id="b"></p>
<div data-wrapper="true" style="font-family:'Segoe UI','Helvetica Neue',sans-serif; font- size:9pt">
<div>test</div>
</div>
<p></p>
<p id="c">66666</p>
</div>
I’m looking to store the test value into a variable or within the id value of b.
You can use querySelector to target the paragraph with the b
id.
Then use nextElementSibling to get the div
right after and finally innerText to get the expected information.
Optionnally, It may be a good idea to use trim to remove any spaces before or after that text.
const myVariable = document.querySelector('#b').nextElementSibling.innerText.trim()
console.log(myVariable)
.hidden{
display: none;
}
<div class="hidden">
<p id="a">66 message</p>
<p id="b"></p>
<div data-wrapper="true" style="font-family:'Segoe UI','Helvetica Neue',sans-serif; font-size:9pt">
<div>test</div>
</div>
<p></p>
<p id="c">66666</p>
</div>
0
I am wondering why you did not add an id
to the div
itself.
But then we can get the b
id with id selector
const myVariable = document.getElementById('b').nextElementSibling.innerText // getting the content of `test` from the div
console.log(myVariable);
document.getElementById('b').innerText = myVariable // storing in the `b` id
.hidden{
display: none;
}
<div class="hidden">
<p id="a">66 message</p>
<p id="b"></p>
<div data-wrapper="true" style="font-family:'Segoe UI','Helvetica Neue',sans-serif; font-size:9pt">
<div>test</div>
</div>
<p></p>
<p id="c">66666</p>
</div>