AndrewS
AndrewS

Reputation: 1565

What is the correct way to use PARENT and FIND?

I try to get the value of input type=text by finding it in the parent DIV with class "half_weight". But I get the result NaN and it should be 0.8.

This is HTML:

<div class="add_controller amt no_space half_weight">                   
  <div class="col s2 adds">
    <a id="" href="#" class="button-minus product_quantity_down">
      <i class="material-icons">remove</i>
    </a>
  </div>
  <div class="col s2 adds">
    <a href="#" class="button-plus product_quantity_up">
      <i class="material-icons">add</i>
    </a>
  </div>
  <div class="col s5 qty">
    <div class="row">
      <span class="col s6 qty_value">
        <input type="text" name="" id="" stepquan="0.8" rtype="2" value="0.8" class="text" />  
      </span>          
  </div>
</div>

This is my jQuery:

$('.button-plus').on('click', function() {
    valTemp = parseFloat($(this).parent('.half_weight').find('.qty_value').siblings('input[type=text]').val());
    alert(valTemp);
});

Upvotes: 1

Views: 64

Answers (1)

curveball
curveball

Reputation: 4505

You don't need siblings() (it refers to neighbors and you have got parent-child relationship). I put just

$(this).parents(".half_weight").find('.qty_value input[type=text]').val();

and it shows 0.8.

$('.button-plus').on('click', function() {
    var valTemp = parseFloat($(this).parents(".half_weight").find('.qty_value input[type=text]').val());
    alert(valTemp);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="add_controller amt no_space half_weight">                   
  <div class="col s2 adds">
        <a id="" href="#" class="button-minus product_quantity_down">
            <i class="material-icons">remove</i>
        </a>
  </div>
  <div class="col s2 adds">
      <a href="#" class="button-plus product_quantity_up">
        <i class="material-icons">add</i>
      </a>
  </div>
  <div class="col s5 qty">
    <div class="row">
        <span class="col s6 qty_value">
            <input type="text" name="" id="" stepquan="0.8" rtype="2" value="0.8" class="text" />  
        </span>

  </div>
</div>

Upvotes: 3

Related Questions