Search code examples
javascripthtmlplist

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?


Solution

  • In pure JS it's

    <key id="myId" class="ignoreTheClass"></key>
    <string>MyText</string>
    
    <script>
        console.log(document.getElementById("myId").nextElementSibling);
    </script>