3

I am using JavaScript with jQuery. I have the following

var $ticket = $nxt.parent().prev().children('.ticket');

and $ticket.text() is integer 1.

I want to increment it by 1 when when I click as;

$('.next').click(function()

How can I do this??

6
  • Have you read the .html() documentation? api.jquery.com/html (although .text() would be better here imo). Commented Jun 24, 2011 at 8:36
  • I changed it.. now hav a look at.. Commented Jun 24, 2011 at 8:38
  • Still, read the documentation about api.jquery.com/text and you will know how to set the value. Commented Jun 24, 2011 at 8:39
  • is there any chance for something like $ticket.data("currentIndex", 1); $ticket.data("currentIndex", $ticket.data("currentIndex") + 1); Commented Jun 24, 2011 at 8:58
  • 1
    Yes that works (try it). But it will not updated the value displayed in the cell. Commented Jun 24, 2011 at 9:00

4 Answers 4

4

A fancy solution would be

$.fn.extend({
  increment: function () {
    return this.text( function (i, currentText) {
      return parseInt(currentText, 10) + 1;
    });
  }
});

In your case use as:

$nxt.parent().prev().children('.ticket').increment();

Also see this jsFiddle.

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

4 Comments

nb: jQuery plugins are supposed to work on multiple elements - this one won't.
@Tomalak ah yes, sneaky - using the function-based .text() call. I was looking for the .each() that is normally needed to iterate over a jQuery object. Nice :)
@Tomalak your inner callback is more complicated than it needs to be - .text(f) callback is supplied and returns the new text - should just be return parseInt(t, 10) + 1 - see jsfiddle.net/alnitak/kcpLV
@Alnitak: Thanks. :) Fixed my answer accordingly.
1

Somewhat inspired by the original version of Tomalak's answer, you can use the callback-based version of .text to change the value in a single jQuery call:

$ticket.text(function(i, t) {
    return parseInt(t, 10) + 1;
});

For those of you that tried to use ++, it won't work for two reasons:

  1. You can only use ++ on variables, not the return value of a function
  2. Even then, in postfix mode it would have returned the old value, not the incremented value

Comments

0
$ticket.click(function(e) { 
    $(this).val(parseInt($(this).val()) + 1); 
    e.preventDefault(); 
});

2 Comments

Same here, .val() only works on form field and $ticket does not appear to be a form field.
What happens if the value of this is 08? :P
-1

Try this

 $ticket.val($ticket.val()+1)   

EDIT

$ticket.text(parseInt($ticket.text(),10)+1);

If the element is somwhere other than a form

Sorry for the mistake, ++ doesnt work here. Have edited the solution accordingly.

8 Comments

Only works for form fields. As far as I can tell, $ticket is not a form field.
$ticket.text($ticket.text()+1); is adding 1 at the end of value.. why is this happening and how to fix??
actually check it, it is ' parseInt($ticket.text())+1 ' you left out the parseInt part
I get the error ReferenceError: Invalid left-hand side expression in postfix operation for using ++ this way. You can only use it on variables.
@Balanivash no - if undefined a number starting 0 may be parsed as octal and one starting 0x as hex. developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
|

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.