Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the next element with a specific class after a specific element

I have a HTML markup like this:

<p>
  <label>Arrive</label>
  <input id="from-date1" class="from-date calender" type="text" />
</p>

<p>
  <label>Depart</label>
  <input id="to-date1" class="to-date calender" type="text" />
</p>

<p>
  <label>Arrive</label>
  <input id="from-date2" class="from-date calender" type="text" />
</p>

<p>
  <label>Depart</label>
  <input id="to-date2" class="to-date calender" type="text" />
</p>

I want to get the next element after from dates to get the corresponding to date. (Layout is a little more complex but from date has from-date class and to date has to-date class).

This is I am trying to do, I want to take a from date element and find the next element in the dom with to-date class. I tried this:

$('#from-date1').next('.to-date')

but it is giving me empty jQuery element. I think this is because next gives the next sibling matching the selector. How can I get the corresponding to-date?

like image 270
rubyprince Avatar asked Jul 19 '12 11:07

rubyprince


People also ask

How do you get the next class element?

To get the next element with a class attribute regardless of how many elements are between it and the current element, use the nextAll() jQuery method. Pass the class name as the first argument. var next = $('. second').

How do I target the next class in jQuery?

jQuery next() Method The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.

What is nextSibling in JavaScript?

nextSibling returns the next node (an element node, a text node or a comment node). Whitespace between elements are also text nodes. nextElementSibling returns the next element (not text and comment nodes).


2 Answers

Couldn't find a direct way of doing this, so wrote a little recursive algorithm for this.

Demo: http://jsfiddle.net/sHGDP/

nextInDOM() function takes 2 arguments namely the element to start looking from and the selector to match.

instead of

$('#from-date1').next('.to-date')

you can use:

nextInDOM('.to-date', $('#from-date1'))

Code

function nextInDOM(_selector, _subject) {
    var next = getNext(_subject);
    while(next.length != 0) {
        var found = searchFor(_selector, next);
        if(found != null) return found;
        next = getNext(next);
    }
    return null;
}
function getNext(_subject) {
    if(_subject.next().length > 0) return _subject.next();
    return getNext(_subject.parent());
}
function searchFor(_selector, _subject) {
    if(_subject.is(_selector)) return _subject;
    else {
        var found = null;
        _subject.children().each(function() {
            found = searchFor(_selector, $(this));
            if(found != null) return false;
        });
        return found;
    }
    return null; // will/should never get here
}
like image 61
techfoobar Avatar answered Sep 25 '22 08:09

techfoobar


.next('.to-date') does not return anything, because you have an additional p in between

You need .parent().next().find('.to-date').

You might have to adjust this if your dom is more complicated than your example. But essentially it boils down to something like this:

$(".from-date").each(function(){
    // for each "from-date" input
    console.log($(this));
    // find the according "to-date" input
    console.log($(this).parent().next().find(".to-date"));
});

edit: It's much better and faster to just look for the ID. The following code searches all from-dates and gets the according to-dates:

function getDeparture(el){
    var toId = "#to-date"+el.attr("id").replace("from-date","");
    //do something with the value here
    console.log($(toId).val());
}

var id = "#from-date",
    i = 0;

while($(id+(++i)).length){
    getDeparture($(id+i));
}

Take a look at the example.

like image 23
Christoph Avatar answered Sep 22 '22 08:09

Christoph