Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select an element directly after an element with id

I have this html:

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

I am aiming to be able to change the label element in some way (e.g - changing colour, edit text, etc.) without having to add an attribute to it, but I would like to do so in a way it selects any element, that is the first element directly after the #myId. This is required, because this will be used in a program used to generate mobileconfig files, which means this will need to be used more than once. If the answer includes putting the first element after the <key> into a child element, the mobileconfig will be invalid.

Could this all be done in just JS?

like image 860
Cat Overlord Avatar asked Oct 18 '25 13:10

Cat Overlord


1 Answers

In pure JS it's

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

<script>
    console.log(document.getElementById("myId").nextElementSibling);
</script>
like image 143
amitka Avatar answered Oct 21 '25 02:10

amitka