How to get numeric value inside <td> - javascript

Trying to calculate sum of checked tr's.
var totalqt=0;
totalqt=totalqt + $(this).closest("tr").find("#qt").text();
It gets correct values but doesn't operate with it like digits. for ex, if value was 1 for first td, and 2 for second td, it alerts 12 instead of 1+2. Tried text() and html(). Same result. What's wrong?

totalqt = totalqt + parseInt($(this).closest("tr").find("#qt").text(), 10);

You need to parse the value as a number this will either be using parseInt(val, base) or parseFloat(val, base):
In your example you'd use:
var totalqt=0;
totalqt=totalqt +parseInt( $(this).closest("tr").find("#qt").text(), 10);

You need to parse the string to an int so that you can use it like an int.
var totalqt=0;
totalqt=totalqt + parseInt($(this).closest("tr").find("#qt").text(), 10);
The 10 is because:
The problem is with how parseInt guesses the base of your number. Read
the parseInt spec. Instead of always defaulting to base 10, it tries
to guess, and if the first character is '0' it thinks you want to
parse as an octal number, and if it starts with '0x' it thinks you
want hexadecimal.

text() returns a string. You want a number. Use parseInt:
var totalqt = 0;
totalqt = totalqt + parseInt($(this).closest("tr").find("#qt").text(), 10);

Related

how to get value of an input box in html and do operations on it

I was testing a simple code to:
Get value of an input box
Do Mathematical operations on it...
My Code:
setInterval(function(){var a=document.getElementById("myInputBox").value;var b=a+1;a.value=b;},1000);
I am trying to add 1 to the value of that input box, every 1 second.
For example, if the box has value 1 ,then after 1 second it should become 2,....and so on.....
But it gets joined with the box's value, like 11 instead of 2.
Why is that?
Try with full code & demo:
get value from inputbox is a string, so it can't calculate mathematically. so, at first convert string to numeric by using function parseInt and then calculate and put into box.
setInterval(function() {
var box = document.getElementById("myInputBox");
var r = parseInt(box.value, 10) + 1;
box.value = r;
}, 1000);
<input type="text" id="myInputBox" value="1" />
var b = parseInt(a) + 1;
a is a string, so the + is interpreted as string concatenation by default. To fix, pass a to parseInt because it returns a type integer. Now the + becomes a math operator.
Extra Credit:
var b = parseInt(a, 10) + 1;
For an extra measure of integrity, pass the second (radix) argument, ensuring base 10 is used to determine the return integer value.

How to add one to a variable to make 2 not 11

Currently this makes 11. It's for a slideshow and the var "n" equals 1 by default
function forward() {
document.getElementsByClassName("img")[0].setAttribute("class","imgout");
setTimeout( function() {
var n1 = document.getElementById("img").getAttribute("data-number");
var n=n1+1;
document.getElementById("img").setAttribute("data-number", n);
document.getElementById("img").setAttribute("src", "images/" + n + ".jpg");
document.getElementsByClassName("imgout")[0].setAttribute("class","img");
}, 500)
}
Use parseInt():
var n = parseInt(n1, 10) + 1;
Instead of:
var n=n1+1;
When n1 will be a string, because it came from the DOM, you need to convert n1 to an integer. There are many ways to do this, and really you should probably use a regular expression to validate that n1 contains what you expect first, but that being said you can try any of the following:
var n=parseInt(n1, 10)+1;
var n=(n1*1)+1;
var n=(+n1)+1;
As an aside the regex for validating the input from the DOM might be something such as:
/^-?\d+$/
Use Number():
var n = Number("1");
FIDDLE
parseInt - good choice. Also you can do
var n = n-0+1
Just remember that of you have different types "+" will convert to string and "-" will convert to numbers
Convert n1 to number as it is a string like ~~n1. The ~~n1 form is good if you know you want an integer (a 32-bit integer).
Second way is to use Number() function i.e. Number(n1) will convert n1 value into a string.
var n=parseInt(n)+1;
Documentation
Fiddle

Change count down to count up

This script counts down from 60 to 0 and stops when it reaches 0.
<script type="text/javascript">
var counttx= "60";
var counterrx=setInterval(timerrx, 1000); //1000 will run it every 1 second
function timerrx()
{
counttx=counttx-1;
if (counttx < 0)
{
clearInterval(counterrx);
return;
}
document.getElementById("timerrx").innerHTML=counttx; // watch for spelling
}
</script>
Instead of counting down, I want the script to count up. I changed the - to a + in counttx=counttx-1; but then the following happend:
60
601
6011
60111
etc.
Looks like counttx is a string, and javascript is appending '1'. Try:
counttx = +counttx + 1;
Edit: or just remove the quotes in the var statement:
var counttx = 60;
If one operand of - operator is string and another is number JS converts the string to number. Thats why count down is working even if counttx is string. But when one operand of + is string and another is number JS converts the number to string and does a string concatenation. Thus you are getting 601, 6011 etc. instead of count up. To fix this you can declare counttx as integer.
var counttx = 60;
Remove the quotes from var countxx="60";
Write it as
var countxx=60;
and then change it to
counttx=counttx+1;
When you mention the value within quotes, it considers it as string and just appends 1 to the value, that is the reason your getting 601, 6011 etc.
You need to parse var to integer first then increment it. Use counttx = parseInt(counttx) + 1;
Do you want to count from 0 to 60? If so swap the 60 and the 0 in your script and change this line counttx = counttx + 1; to counttx = counttx - 1. Also get rid of the double quotes around 60.

String format in javascript

I'd like to format a number and remove starting 0s from it. e.g.:
001 => 1
10 => 10
Actually, I have a jQuery method which does the following:
$('#myelement').text($('#myElement').text()+1);
When the element's text is 0, the function makes it 01, How can I make it to show 1 using jQuery or javascript?
Thanks.
what you need:
parseInt($('#myElement').text(), 10) + 1;
what you're asking for:
($('#myElement').text()+1).replace(/^0+/, '');
You can use parseInt("0010", 10).toString(). Ignore toString() if you need the int only.
Assuming you want to increment the value, convert the current text in to a number using unary +:
$('#myelement').text(function(i, v){
return +v + 1;
});
+"0001" //1
See above! .......
$('#myelement').text(parseInt($('#myElement').text(), 10) + 1);

jquery , ajax data sum cant get to work correctly

$.post('ajax_ceneizbaze.php', function(cenovnik){
if(cenovnik){
cenastr=cenovnik.cenastrana;
cenadinamika=cenovnik.cenadinamika;
cenabaza=cenovnik.cenabaza;
cenakorpa = cenovnik.cenakorpa;
cenacms = cenovnik.cenacms;
inkrementodrzavanje = cenovnik.cenaodrzavanje;
rezz = parseInt(cenastr+cenadinamika);
alert(rezz);
}
else alert('bla bla..');
},'json');
initial value for cenastr is 25, and for cenadinamika is 50 ,Ajax works perfectly in this mine example, but when i try to sum values cenastr and cenadinamika i get output 2550 , instead 75? why i cant convert that to integer and to get sum of thoose two. it only output result in string format. i tried parseInt to place before sum operation but it doesnt helps.
you have to parseInt each of the strings:
rezz = parseInt(cenastr) + parseInt(cenadinamika);
Try that out
http://www.javascripter.net/faq/convert2.htm - this might help. You need to convert the strings to numbers before the calculation!
The + operator has a dual purpose. On strings it concatenates them:
"25" + "50" = "2550"
With numbers, it sums them.
25 + 50 = 75
Therefore we can deduce that your two variables are strings, and that you parse the result of concatenating them to an integer, giving you 2550.
You need to parse each individual value to an int before using the + operator to add them:
rezz = parseInt(cenastr,10) + parseInt(cenadinamika,10);
Make sure the variable are numbers before adding:
cenastr= +cenovnik.cenastrana;
cenadinamika= +cenovnik.cenadinamika;
//...
rezz = cenastr + cenadinamika;
parseInt will be working on the result of the addition, which, both being strings, will be the concatenation.
Either:
parseInt(cenastr) + parseInt(cenadinamika)
or use the unary operator:
(+censtr) + (+cenadinamika);

Categories

Resources