Home > Software design >  How to select child element based on parent element using java script query selector
How to select child element based on parent element using java script query selector

Time:10-22

my page multiple element have same class like below

<div>
 <ol class="sample-class">
  <li>list1</li>
  <li>list2</li>
  <li>list3</li>
 </ol>
 <div class="help">condent 1</div>
</div>
....
</div>
 <ol class="sample-class">
   <li>list1</li>
   <li>list2</li>
   <li>list3</li>
  </ol>
 <div class="help">condent 2</div>
</div

I need to change the help content based on sample-class I js to select the element but it is not working.

let listVal = document.getElementsByClassName('sample-class');
for(let el of listVal) {
 el.offsetParent.getElementsByClassName('help')[0].innerHTML = "my dynamic condent"
}

anyone help me. Thanks

CodePudding user response:

You cannot use getElementById to find something inside another element, so you need to use getElementByClassName instead. Also, as mentioned in one of the comments, the ID should be a unique. No 2 elements can have the same ID.

Kindly check the below snippet which would work as expected.

let listVal = document.getElementsByClassName('sample-class');
for(let el of listVal) {
 el.getElementsByClassName('help')[0].innerHTML = "my dynamic condent"
}
<div>
    <div class="sample-class">
        <ol>
            <li>list1</li>
            <li>list2</li>
            <li>list3</li>
        </ol>
        <div class="help">condent 1</div>
    </div>
    <div class="sample-class">
        <ol>
            <li>list1</li>
            <li>list2</li>
            <li>list3</li>
        </ol>
        <div class="help">condent 2</div>
    </div>
</div>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You have overcomplecated your code by looping over an array and not modifing the array you are looping over. I also like to state that when accessing getElementById you always get the first DOM element containing this ID. To fix this issue change the id's to classes and loop over these classes instead of the siblings

const elements = document.querySelectorAll(".help");
for (const element of elements) {
  element.innerText = "new text"
}

If for some reason you need to loop over the ol elements:

const elements = document.querySelectorAll("ol");
for (const element of elements) {
  element.nextSibling.innerText = "new text"
}

CodePudding user response:

ID Should be unique on a page and use the nextSibling function.

<div>
 <ol class="sample-class">
  <li>list1</li>
  <li>list2</li>
  <li>list3</li>
 </ol>
 <div class="help">condent 1</div>
</div>
....
</div>
 <ol class="sample-class">
   <li>list1</li>
   <li>list2</li>
   <li>list3</li>
  </ol>
 <div class="help">condent 2</div>
</div>
<script>
let listVal = document.getElementsByClassName('sample-class');
for(let el of listVal) {
 el.nextSibling.innerHTML = "my dynamic condent"
}
</scritp>

Note: If you add whitespace between the two ol elements, the result will be "undefined". Thanks

CodePudding user response:

You can create a new querySelector in the for loop to select the element with the help class/id, you can achieve this by doing the following:

1 By using parentElement

const elements = document.querySelectorAll(".sample-class");
for (const element of elements) {
  element.parentElement.querySelector(".help").innerText = "new text"
}

2 By using sibling selector

const elements = document.querySelectorAll(".sample-class");
for (const element of elements) {
  element.querySelector("~ .help").innerText = "new text"
}
  • Related