Take value and date attr of related element jQuery

Asked

Viewed 563 times

1

I’m having a little problem with jQuery, I have the following order of Divs:

$(document).on('click', '.btn_salvar_obg_acess_prazo', function() {
  var data = $(this).prev('.acess_prazo_date').val();
  alert(data);
});
<div class="row">
  <div class="col-md-12">
    <div class="col-md-3">
      <span><?php echo $obrigacao_correspondente_nome ?></span>
    </div>
    <div class="col-md-6">
      <input type="text" data-m='<?php echo $m_c ?>' data-a='<?php echo $a_c ?>' data-obg='<?php echo $obrigacao_correspondente ?>' value="<?php echo $data ?>" name="obg_acess_prazo" class="acess_prazo_date datepicker_full form-control">
      <hr>
    </div>
    <div class="col-md-2">
      <button class="btn btn-primary btn_salvar_obg_acess_prazo">Salvar</button>
      <hr>

    </div>
  </div>
</div>
These Rows are generated through a query in mysql, and there may be more than one, I need that when you click the 'Save' button it takes the value of the date-[] attributes and the value of the previous input.

I’ve tried with '.find()', '.Parent()', '.Prev()' and still can’t, always results in 'Undefined'.

  • Post full html output, no php. Please!

  • There is only 1 input per .row?

1 answer

1


Forehead to wear

var data = $(this).closest('div').prev().find('.acess_prazo_date').val();

if there are several inputs in each .row, or if there is only 1:

var data = $(this).closest('.row').find('.acess_prazo_date').val();

Explanation:

First variant:

Climb the DOM to the div, (in this case could be only .parent(), but thus is proof of future changes) then seeks the previous element (among siblings/siblings) and finally descends in the GIFT looking for .acess_prazo_date.

Second variant:

With the .closest('.row') it picks directly element with class .row closer, and then with .find('.acess_prazo_date') he searches within that element the element with the class .acess_prazo_date.

The .val() return the value of input.

  • I got it with var data = $(this). Parent('div'). Parent('div'). find('.acess_prazo_date'). val(); But the second option you gave worked, and with fewer actions, worth!

Browser other questions tagged

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