0

When an event happens i want to reduce the value of an input named "shareNum" by 1. This is what i currently use:

$( "#shareNum" ).val(($(this).val-1) );

I get a "NaN" - a non value.. something is wrong, any thoughts?

2
  • If someone is able to do the same thing on a <span> tag or any other, that would be great Commented Apr 16, 2011 at 22:36
  • It would be almost exactly the same as @Darin's posted answer, except using text() rather than val(). And, obviously, with sanity checks to ensure it's a valid number. Commented Apr 16, 2011 at 22:39

2 Answers 2

1

Try like this:

var $shareNum = $("#shareNum");
$shareNum.val(Number($shareNum.val()) - 1);

And here's a live demo.

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

2 Comments

@Tom, could you post a JS Fiddle that reproduces the problematic parts of your application?
@Tom, how does the code for your application look like then? Did you check the live demo I provided? How does your code differ?
0

This should do it:

$('#shareNum').val(function(i, v) {
    return v - 1;
});

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.