Get value and date attr from jQuery related element

1

I have a small 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 when I click the 'Save' button it takes the value of the data- [] attributes and the value of the previous input.

I have tried with '.find ()', '.parent ()', '.prev ()' and still can not, always results in 'undefined'.

    
asked by anonymous 10.04.2017 / 20:46

1 answer

1

Test to use

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

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

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

Explanation:

First variant:

Go up the DOM up to div , (in this case it could be only .parent() , but this is proof of future changes) then look for the previous element (between brothers / siblings) and finally descend in the DOM to search for .acess_prazo_date .

Second variant:

With .closest('.row') , it directly picks up element with the closest .row class, and then with .find('.acess_prazo_date') it looks for that element with the .acess_prazo_date class.

The .val() returns the value of the input.

    
10.04.2017 / 20:50