0

I want to update the value of text box from where I'm getting initial value to add product to cart. Now I'm applying round function & I want to update the value of text box that is because I'm using ajax & if I'm applying round function, user must know how system calculated everything.

Here's my simple code:

<script>
$(document).ready(function(){
$(document).delegate('.purchasenow','click', function(e){
    var buynow=this;
    var productid = $(this).attr('id');
    var quantity = $('.quo_'+productid).val();
    var quantity = Math.round(quantity);
    alert(quantity); //This gives the value
    $('.quo_'+productid).value = quantity; //This is not working
});
});

Can anyone tell why it's not working? It's very simple but I'm not able to find out the cause.

5
  • can you create jfiddle? Commented Jul 7, 2014 at 5:01
  • 1
    Because a jQuery object doesn't have access to the DOM methods. jQuery uses .val(), the DOM uses .value. Commented Jul 7, 2014 at 5:01
  • Yes agree to it, however $('.quo_'+productid).val(quantity); this works Commented Jul 7, 2014 at 5:03
  • Well yes; of course it does: that's because .val(quantity) is a jQuery method. Which is available to a jQuery object... Commented Jul 7, 2014 at 5:06
  • Yup, that was I missing, thanks a lot for info :) Commented Jul 7, 2014 at 5:06

2 Answers 2

1

Have you tried

$('.quo_'+productid).val(quantity);

The jQuery selector returns a wrapped object, not the actual DOM element. I don't think wrapped object has the .value property

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

1 Comment

This works, I'd never tried this, I was using my method for all the value change. Thanks a lot :)
1

you are almost correct the only thing why it is not working because your syntax is wrong

this is the correct syntax for jQuery

$('.quo_'+productid).val(quantity);

if you want it in javascript

var txt = document.getElementById("quo_"+productid);
txt.value = quantity;

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.