0

I'm having problems with incremental values without refreshing the page using jquery.

I have html code that does this.

<li class=''>
<img src="../icons/fav_c.png" width='12' style='vertical-align:middle;'/>
&nbsp;&nbsp;
<div style='float:right;padding-top:2px;width:5px'>
<span class='selector'>
<?php echo $value; ?>
</span>
</div>
</li>

I have a code that does in jquery.

val value = $('span.selector').text();
$('span.selector').text(value++);

This is not affecting my value that is being echo'd out? Can anyone see what the problem is? Does .text() not work?

Thanks!

1
  • Put var instead of val Commented Aug 20, 2011 at 23:20

4 Answers 4

4

Ok:

var value = $('span.selector').text();
value = parseInt(value, 10); // Use a radix!!!
$('span.selector').text(++value);
  1. ) parseInt with radix with out it '09' for example would be 0. Converts string to an integer

  2. ) ++value - increment value before it's applied to .text(...). value++ would call text() first then increment.

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

1 Comment

Sorry, i was going to accept it but I had a time limit. and i just lost track of time. Thanks
2

Try:

var value = $('span.selector').text();
value++;
$('span.selector').text(value);

Fiddle

Comments

2

.text expects a string

Description: Get the combined text contents of each element in the set of matched elements, including their descendants.

http://api.jquery.com/text/

try

var value = $('span.selector').text();
value = parseInt(value);
value++;
$('span.selector').text(value++);

here is the fiddle http://jsfiddle.net/XGGPw/

Comments

1

You can also try this

$("span.selector").text(function(i, v) {return parseInt(v, 10) + 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.