Get the next input after an input (object)

1

I want to get the object of the 2nd input. Since I have more inputs with class 'check-date2', I want to get the element immediately after the first.

HTML

<div class="form_line">
                <div class="column-50">
                    <div class="line_desc">
                        Data Início<font>*</font>
                    </div>
                    <div class="line_field">
                        <div class="form-group">
                            <input id="datepicker1"
                                class="form-control  input date-check check-date"
                                placeholder="Data Início - DD-MM-YYYY" type="text"
                                name="event_date_start">
                        </div>
                    </div>
                </div>
                <div class="column-50">
                    <div class="line_desc">
                        Data Fim<font>*</font>
                    </div>
                    <div class="line_field">
                        <div class="form-group">
                            <input id="datepicker2"
                                class="form-control input date-check check-date2"
                                placeholder="Data Fim - DD-MM-YYYY" type="text"
                                name="event_date_end">
                        </div>
                    </div>
                </div>
            </div>

No. I have this:

$(".check-date").on("change", function() {
    checkIfSecondDateIsGreaterThanFirst(this);
});

And the function that should take the input object:

function checkIfSecondDateIsGreaterThanFirst(date) {
/**
 * to verify that the second data is greater than the first
 */
var next_date = $(date).next('.check-date2');
alert($(next_date).attr('class'));

}

    
asked by anonymous 30.08.2016 / 13:35

1 answer

1

If the input is inside the div with class .column-50 and both .column-50 are followed you can do this:

function checkIfSecondDateIsGreaterThanFirst(date) {
    var next_date = $(date).closest('.column-50').next().find('.check-date2');
    alert($(next_date).val('class'));
}

jsFiddle: link

  • .closest() rises in the DOM up to div.column-50
  • .next() selects div.column-50 next
  • .find('.check-date2') chooses the input within this div.column-50
30.08.2016 / 13:50