MyText

How to select an element immediately after an element with id

I have this html:

<key id="myId" class="ignoreTheClass"></key>
<string>MyText</string>

      

I am trying to change the label element in some way (like change color, edit text, etc.) without adding an attribute to it, but I would like to do it in such a way that it selects any element that is the first element directly after #myId

. This is necessary because it will be used in the program used to generate the mobileconfig files, which means that it will need to be used multiple times. If the response includes placing the first item after <key>

the child, the mobileconfig file will be invalid.

Can all this be done in JS only?

+3


source to share


2 answers


In pure JS, this is



<key id="myId" class="ignoreTheClass"></key>
<string>MyText</string>

<script>
    console.log(document.getElementById("myId").nextElementSibling);
</script>

      

+2


source


In JQuery:

$('#myId').next()

      



I think you wanted it.

0


source







All Articles