skip to Main Content

below is the structure of my HTML, generated within D365 Portal, how to store the info of div after the paragraph tab of 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

Trying to get the value of "test" into a variable

2

Answers


  1. 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>
    Login or Signup to reply.
  2. 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>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search