views:

71

answers:

5

I have a hidden input #start thats value is used to display a range of returned data (it is the starting point of an index) each time you click #next I need to increase its value.

$("#next").click(function() {
  $("#start").val() + 80;
)};

is this correct? or is there a better way? thx all!

+1  A: 

All you're doing is retrieving val and adding 80 to it, you're not actually setting it back to the value on the element. To do so try the following:

$("#next").click(function() {
  $("#start").val( parseInt($("#start).val()) + 80 );
)};

This is somewhat inefficient though, as you'll be selecting #start twice. Instead I'd cache the #start selection in a variable:

$("#next").click(function() {
  var startElement = $("#start");
  startElement.val( parseInt(startElement.val()) + 80 );
)};
Soviut
Correct. `val()` is not a property, but a function. With no parameters it returns the value, with a parameter it sets the value.
Fosco
`val` returns a string, not a number. So adding `80` you'll end up with `8080`
fudgey
I've corrected the code to include a parseInt.
Soviut
No need to store the element. You can use this.value inside of .val() to retrieve its current value.
John Strickler
A: 

Use a little closure pattern:

$("#next").click(function cli() {
    var v = 0;

    return function() {
        $("#start").html(v += 80);
    };
}());
jAndy
It will set 80 each time button clicked.
NAVEED
@NAVEED: fixed.
jAndy
+2  A: 

You need to convert the value to an integer, add whatever number to it, and save it back:

$("#next").click(function(){
      var startElement = $("#start");
      var value = parseInt(startElement.val(), 10);
      startElement.val(value + 80);    
});

Working sample: http://jsfiddle.net/QZgAf/

KP
@KP this will change the value for #start correct?
Dirty Bird Design
You should include a radix with the `parseInt` function to ensure the returned value is base 10 (http://www.w3schools.com/jsref/jsref_parseInt.asp)
fudgey
@fudgey Edit it and fix it yourself (I just did because I wholeheartedly agree with you). Don't be scurred; that's what this community is all about.
Josh Stodola
@KP To see what we're talking about, put the initial value of the hidden field to "080" and run it. You will not see 160 in the alert as expected.
Josh Stodola
@Josh - With all due respect, that's fine if you want to improve someone's answer, but @fudgey (or anyone else) isn't obligated to do so. He could have given it a down-vote for its flaws, but instead took the time to comment. That's what this community is all about. ;o)
patrick dw
Sooo which one is going to give the least amount of problems?
Dirty Bird Design
@Dirty - Depends on the initial value. Overall, I'd probably use fudgey's, or jAndy's if you know you have a `0` starting point. Or if you know you'll never get a hexadecimal value in the field, I'd use mine, mostly because it doesn't create another jQuery object on each click.
patrick dw
@patrick With all due respect, now that the edit is fixed, fudgey's comment (and many comments thereafter (including this one, lol)) is pretty much... noise. Irrelevant and unproductive, a waste of resources. I don't see any point in calling somebody out on a little mistake (in an already accepted answer) when you can just edit the answer and add three characters (the person who answered gets notified of this). It's a wonderful system! And we all are entitled to our own opinions, and being able to express them is another wonderful thing. I was definitely not attacking fudgey! Cheers :)
Josh Stodola
@Josh - I certainly don't think you were attacking fudgey. :o) Although I wouldn't agree that a discussion about the answer is noise. On that note, it will still fail if the `#start` input has an empty string. Someone should really fix that! ;o)
patrick dw
Thanks for the edit Josh. Didn't realize that was required by I wholeheartedly agree.
KP
+6  A: 

Try this (demo):

$("#next").click(function() {
  $("#start").val(function(i,v){
   return parseInt(v,10) + 80 || 0;
  });
});
fudgey
This example needs a better explanation of what's going on.
Soviut
@Soviut: Basically you can add a function to `val`. The `i` is the index and `v` is the value as set by jQuery (http://api.jquery.com/val/). So it will parse `v` to get the integer value which is then added to 80 (but initially the value is undefined, so gives a `NaN` which can't be added to 80, so it returns 0 instead).
fudgey
@fudgey how is this different than @KP's? I don't understand this enough to know the pitfalls!
Dirty Bird Design
+1 Nice answer. Avoids possible octal values and `NaN` issues.
patrick dw
@Dirty Bird Design: Our answers are very similar. The two differences are the `,10` inside the `parseInt` to insure the value is base 10, and the `|| 0` which returns a zero if the previous value is not defined - which is the case in my demo because the hidden input doesn't have a value initially.
fudgey
I meant the answer itself should explain what's going on in the code example. It is far from obvious and can use some documentation.
Soviut
+1  A: 

Here's another way. It accesses the value attribute directly and uses ~~ to avoid the possible octal or NaN issues.

$("#next").click(function() {
     var start = document.getElementById("start");
     start.value = ~~start.value + 80;
});

Or better is to only run the selector for #start once if the button could be clicked more than once.

   // cache "start" outside the handler
var start = document.getElementById("start");

$("#next").click(function() {
     start.value = ~~start.value + 80;
});
patrick dw
`~~` would not protect from a value like "0x22"
jAndy
@jAndy - Yes, but what are the odds? :o) My only intention for `~~` was to protect from an empty string. It appears as though the sole purpose of the hidden input is for the increment. So given the apparent context, `~~` should be fine.
patrick dw
@patrick: against all odds... :p
jAndy
@patrick: wow you just blew my mind, what is that `~~` operator ( double not?) called or could you point me to the documentation so I can learn more about it?
fudgey
@fudgey - It is a "double bitwise NOT". Here's a good read: http://james.padolsey.com/javascript/double-bitwise-not/ It will give either a result of an Integer value of the string or float, or `0` if it could not be converted. It will ignore leading zeros, so there's no worry about converting to an octal, but as jAndy pointed out, given "0x22" it will consider that a hexadecimal, and will return "34". I figure if you're passing that number format, you probably intend for it to be converted, but depends on the context I guess. :o)
patrick dw