0

I have a simple plus and minus button next to my input. This works fine. However, it's not actually changing the value once the number is set in the form. This is causing problems since I have a calculator that changes based on the value of my input. Is there a way to make jQuery change the actual value when the plus/minus buttons are used? I can manually type a number and it works fine.

I have tried using .change() and .attr('value' currentVal) with no luck.

http://jsfiddle.net/zer00ne/v1pje44v/

Lynx
  • 1,462
  • 5
  • 32
  • 59
  • Possible duplicate of [How to set value of input text using jQuery](http://stackoverflow.com/questions/10611170/how-to-set-value-of-input-text-using-jquery) – Leon Adler Jan 16 '16 at 04:26
  • Your fiddle works fine for 0-10 numbers. Is that what you expected? Or what is `actual` value? – Nikolay Ermakov Jan 16 '16 at 04:29
  • Yes. That part is working fine. However, it's not actually changing the value. It's still equal to 0. My calculator is checking the value of the input to determine what value to actually calculate. – Lynx Jan 16 '16 at 04:31
  • The actual html input value is not changing there fore his calc is always getting 0 fyi – prola Jan 16 '16 at 04:34
  • So is there no way for jQuery to change that? – Lynx Jan 16 '16 at 04:34
  • Click button in this fiddle - it updates alright: http://jsfiddle.net/ermakovnikolay/j1asaxcy/ – Nikolay Ermakov Jan 16 '16 at 04:36
  • Doesn't make sense. If you see it changed...it's value is changed. Note that the attribute will always be the same, only the value property will change – charlietfl Jan 16 '16 at 04:36
  • Probably something wrong with how you try to get the input value. The `$('#yourInputId').val()` from my fiddle above works fine. – Nikolay Ermakov Jan 16 '16 at 04:39
  • only have to do `$(".qty").val(currentVal - 1)` for set values. – Parth Trivedi Jan 16 '16 at 04:40
  • Please check http://jsfiddle.net/zer00ne/v1pje44v/ . Here in console. your `attribute` `value` is always `0` and `property` `value` is changing. You have to check `attr` and `prop` differance. – Parth Trivedi Jan 16 '16 at 04:46
  • I am trying to change the attribute value. – Lynx Jan 16 '16 at 04:47
  • then you should set `attr('value', 50);` Can it make sense? what is going wrong? – Parth Trivedi Jan 16 '16 at 04:48
  • I tried doing that but still gave me an error. – Lynx Jan 16 '16 at 04:49
  • If I do this `$('input[name=' + fieldName + ']').attr('value', currentVal + 1);` it will not increment on click. – Lynx Jan 16 '16 at 04:50
  • yes for that you need to use `.val()` its a property of input. Don't mix `attr` and `prop` – Parth Trivedi Jan 16 '16 at 04:52
  • So would i set `attr()` in the variable? – Lynx Jan 16 '16 at 04:53
  • No. Only use `.val()` for `property`. – Parth Trivedi Jan 16 '16 at 04:56
  • That's how I currently have it. So what it does is on load it has the value set to 0 as defined in the input. If I change it directly in the textbox it works as normal. however, if I use plus/minus and then click back into the box it will change back to 0. So I need to change what the plus/minus are changing I just don't know how. – Lynx Jan 16 '16 at 04:58
  • The link you shared is working fine.. It is changing with + and - Sign. – Rahul Kumar Jan 16 '16 at 05:11
  • yes, but it is not changing the value of the input. Which is what I need it to do for my calculator reading the value. – Lynx Jan 16 '16 at 05:14

1 Answers1

0

You could use this

Change

var currentVal = parseInt($('input[name=' + fieldName + ']').val();
$('input[name=' + fieldName + ']').val(currentVal + 1);
$('input[name=' + fieldName + ']').val(10);
$('input[name=' + fieldName + ']').val(currentVal - 1);
$('input[name=' + fieldName + ']').val(0);

To

var currentVal = parseInt($('.qty').val());
$('.qty').val(currentVal + 1);
$('.qty').val(10);
$('.qty').val(currentVal - 1);
$('.qty').val(0);

This respective change will work for you.

dingo_d
  • 11,160
  • 11
  • 73
  • 132
Uttam Kumar Roy
  • 2,060
  • 4
  • 23
  • 29
  • I tried `$('input[name=' + fieldName + ']').val(currentVal + 1); trigger('keyup');` with no luck. – Lynx Jan 16 '16 at 04:58
  • Oh sorry! OK that sort of worked. For it to pick it up I have to click back into the box. However, I am not sure if this is the best approach. Since users will be clicking plus or minus they are going to want to see the calculator change as soon as they do so. – Lynx Jan 16 '16 at 05:14
  • @ Lynx I changed my code.I wish it will work for you – Uttam Kumar Roy Jan 16 '16 at 09:24
  • Sorry. I still get the same issue. – Lynx Jan 16 '16 at 19:27