Chris Ian
Chris Ian

Reputation: 771

JQuery get input value

HTML

      <td data-title="Quantity">
         <div class="clearfix quantity r_corners d_inline_middle f_size_medium color_dark m_bottom_10">
           <button class="btn-minus bg_tr d_block f_left" data-item-price="8000.0" data-direction="down">-</button>
           <input min="0" class="f_left" type="text" value="2" name="order[line_items_attributes][0][quantity]" id="order_line_items_attributes_0_quantity">
           <button class="btn-plus bg_tr d_block f_left" data-item-price="8000.0" data-direction="up">+</button>
         </div>
         <div>
           <a href="#" class="color_dark remove-order"><i class="fa fa-times f_size_medium m_right_5"></i>Remove</a><br>
         </div>
     </td>

Javascript

    $('.remove-order').click(function(){
       ????
    });

How to get the value of the input text when remove is click?

Upvotes: 0

Views: 288

Answers (4)

user2575725
user2575725

Reputation:

Try closest():

$('.remove-order').on('click', function(e){
  alert($(this).closest('td').find('input[name^=order]').eq(0).val());
});

Here,

  • .closest('td'), the nearest parent td.
  • .find('input[name^=order]'), all input with name attribute starting with order
  • .eq(0), first item in the list

Upvotes: 0

Mark Schultheiss
Mark Schultheiss

Reputation: 34227

$('.remove-order').click(function () {d
    var myval="";
    myval = $('input.f_left').val();
    // or use:
    myval = $('input.f_left')[0].value;
});

In this case those would both return the 2 value as a string. IF you want a number be sure to parse it with parseInt(myval);

NOTE: since you have an ID you should use that selector:

var myvalInt = parseInt($('#order_line_items_attributes_0_quantity').val(), 10);

as that would be the fastest method.

Upvotes: 1

Robin Carlo Catacutan
Robin Carlo Catacutan

Reputation: 13699

 $('.remove-order').click(function(){
     var input_val = $('#order_line_items_attributes_0_quantit').val();
     console.log(input_val);
 });

Upvotes: 1

Praveen Singh
Praveen Singh

Reputation: 2567

You can use .val()

$('.remove-order').click(function(){
   $('#order_line_items_attributes_0_quantity').val()
});

Upvotes: 1

Related Questions