0

I want to get an updated value from an input field. So I setup a listener

$('.product-quantity input').change( function() {


console.log($(this).parents('.product')[0].id)
var id  = $(this).parents('.product')[0].id;
 var classgroup = $(this).parents('.product')[0]
 for (var i = 0; i < classgroup.childNodes.length; i++){

    if (classgroup.childNodes[i].className == "product-quantity") {

      console.log(classgroup.childNodes[i])
      var target = classgroup.childNodes[i]

    }
 }
 });

classgroup.childNodes[i] output this html

 <div class="product-quantity">
  <input type="number" id="productquantity" value="1" min="1">
 </div>

what I want to do now is get the user input in value. It is suppose to be simple but I can't figure it out I've tried these but no success console.log(target.attr('value'))

      target.each(function(){
        console.log(this.value)
      })

how can I get the value number for the output html?

3
  • FYI, you can replace $(this).parents('.product')[0] with this.parentNode.closest(".product") in newer browsers. And you can remove the .parentNode if this doesn't have that class. Commented Jan 8, 2018 at 17:35
  • @rockstar is the later compatible with older browser? Commented Jan 8, 2018 at 17:36
  • No, you'd need a polyfill for that. Better than loading a large library though, IMO. Commented Jan 8, 2018 at 17:37

1 Answer 1

1

Is this what you are trying to do? You can grab the val of the input itself, and log on change (or whatever you would like to do).

$('#productquantity').change(function() {
  console.log($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="product-quantity">
  <input type="number" id="productquantity" value="1" min="1">
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.