Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript equivalent to JQuery .next()

Is there a JavaScript method similar to jQuery .next()? I want to find the next element that has the class of "error" relative to the element. I've tried using .nextSibling as a loop but couldn't figure it out. Didn't know if there was an easier way to go about it without jQuery.

For instance, if I have this code:

<div id="section">
    <div id="test">test</div>
    <span class="info">Information</span>
    <span class="error">Error!</span>
</div>

I'm trying to get the next .error class closest to #test, if I have a #section2 and a #test2 I would want to get the .error class closest to #test2 and so on.

like image 315
Howdy_McGee Avatar asked Dec 17 '13 03:12

Howdy_McGee


2 Answers

The nextElementSibling property returns the element immediately following the specified element, in the same tree level.

Example: Get the HTML content of the next sibling of a list item:

var x = document.getElementById("item1").nextElementSibling

The nextElementSibling property might help.

like image 58
swartkatt Avatar answered Sep 28 '22 16:09

swartkatt


Best bet would be to search through the jQuery code and see what they did. http://code.jquery.com/jquery-2.0.3.js

At a glance, I do see some calls to "nextSibling" and "previousSibling."

Also see here: How to get next element using JavaScript-only?

Hope this helps!

like image 32
kmiklas Avatar answered Sep 28 '22 16:09

kmiklas