I have a few elements with numeric IDs. Each ID is in a separate span. Can I get that value as a number in order to increment? I tries as text but it does not seem to work...
var lastVal = $(".myID:last").text();
var newVal = lastVal++;
Value <span class="myID">5</span>
Convert a string to a number in js like so:
var num = parseInt(lastVal, 10);
When you get the .text()
of an element, jQuery returns a string. You can't increment a string, so you'll have to make it an integer using the parseInt()
function:
var lastVal = parseInt($(".myID:last").text(), 10);
Now lastVal
contains an integer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With