Pick up the next input after an input (Object)

Asked

Viewed 322 times

1

I want to get the object of the second input. As I have more inputs with the 'check-date2' class, I want to pick up 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 . js have this:

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

And the function that should pick up 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'));

}

1 answer

1


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

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

jsFiddle: https://jsfiddle.net/rok7cd8z/

  • .closest() goes up in the DOM until the div.column-50
  • .next() selects the div.column-50 next
  • .find('.check-date2') chooses the input within that div.column-50
  • That’s what I wanted, it’s working ?

  • 1

    @M_b_85 in time of .next() uses .prev().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.