0

I have an application where I have to perform a simple calculation where the price of each item * the amount. I am trying to figure out the best way to do it. I have a input type="number" and I have a item price. I need to update my input fied each time the value of the QTY field changes

    $('#add1').click(function () {
    var selected1 = ($('#produceList option:selected').index() - 1);
    myItem1 = pdata[selected1];
    $('#itembox').append('<div id="thep">' + '<p class = "product">' + myItem1.Name + '    ' + myItem1.Price + '</p>' + '<label for="howMAny">Qty:</label>' + '<input id="howMany" type="number" min="0" max="10" value="0">' + '<label for="thisMany">Price:</label>' + '<input type="text" id ="thisMany"readonly>' + '</div>');
2
  • you probably want to utilise the .change() function on the qty field. That will allow you to specify some code to run every time the qty field changes. Something like $("#howMany").change( function () { //recalculate here }); Commented Jan 14, 2014 at 9:52
  • Which element holds the price of the item? Commented Jan 14, 2014 at 9:53

3 Answers 3

1

You can simply use jQuery's change() function. http://api.jquery.com/change/

The change event is sent to an element when its value changes. This event can be used with elements, boxes and elements. For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a selection with the mouse, but for the other element types the event is deferred until the element loses focus.

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

Comments

1

You can use the onchange event for this.

This code should handle it when you are using jQueryY

$('input').on('change', function() {
  alert( this.value );
})

Now, instead of alerting the value you can update your result.

Comments

0

You can write the code in event below. This will be getting triggered everytime textbox for qty changes.

jQuery('#howMany').on('input', function() {
// do your stuff here.
})

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.