Search code examples
javascriptcssgetelementbyid

How to get nextElementSibling type via JS?


I want to check if the nextElementSibling Element is present and if it is a field. But I don't find the correct JS request.

document.getElementById($id).nextElementSibling.type 

does not work.


Solution

  • You need to use nodeName, or tagName.

    var nextElement = document.getElementById('demo').nextElementSibling.nodeName;
    console.log(nextElement);
    <div id="demo">
    <p>
    hola
    </p>
    </div>
    <p>
    next element
    </p>