I'm not good using javascript/jquery so this is a dumb question! I coded this
<li>
<a href=".jpg"><img src=".jpg" width=50 height=50 /></a>
<br><input name="p1" value="p1" prezzo="5" type="checkbox" />
<br><label>Pianta €</label><span class="price">5</span>
</li>
<li>
<a href=".jpg"><img src=".jpg" width=50 height=50 /></a>
<br><input name="p2" value="p2" prezzo="3" type="checkbox" />
<br><label>Pianta €</label><span class="price">3</span>
</li>
var checkValues = $('input[type=checkbox]:checked').map(function() {
return $(this).attr('prezzo');
}).get();
for(var i=0;i<checkValues.length;i++) {
var tot = tot + checkValues[i];
alert(tot);
}
In this way checkvalues contains array of numbers. So checking that 2 checkbox for example, on the browser appear 2 alert:
undefined5
and
undefined53
like if javascript is concatenating numbers like they would be strings!
how can i solve this?
I'm not good using javascript/jquery so this is a dumb question! I coded this
<li>
<a href="http://www.mazzodifiori.it/fiori/P15-Ficus.jpg"><img src="http://www.mazzodifiori.it/fiori/P15-Ficus.jpg" width=50 height=50 /></a>
<br><input name="p1" value="p1" prezzo="5" type="checkbox" />
<br><label>Pianta €</label><span class="price">5</span>
</li>
<li>
<a href="http://www.mazzodifiori.it/fiori/P15-Ficus.jpg"><img src="http://www.mazzodifiori.it/fiori/P15-Ficus.jpg" width=50 height=50 /></a>
<br><input name="p2" value="p2" prezzo="3" type="checkbox" />
<br><label>Pianta €</label><span class="price">3</span>
</li>
var checkValues = $('input[type=checkbox]:checked').map(function() {
return $(this).attr('prezzo');
}).get();
for(var i=0;i<checkValues.length;i++) {
var tot = tot + checkValues[i];
alert(tot);
}
In this way checkvalues contains array of numbers. So checking that 2 checkbox for example, on the browser appear 2 alert:
undefined5
and
undefined53
like if javascript is concatenating numbers like they would be strings!
how can i solve this?
Share edited Jun 11, 2014 at 15:02 Giao Cerquone asked Jun 11, 2014 at 14:52 Giao CerquoneGiao Cerquone 2,4885 gold badges26 silver badges34 bronze badges 4-
1
var tot = tot + checkValues[i];
- thetot
on right hand side is not initialized . hence the result. Put the declaration outside the loopvar tot=0;
– karthikr Commented Jun 11, 2014 at 14:55 -
Why do you think
checkValues
contains an array of numbers? Have you read the documentation for.attr()
? It always returns a string (orundefined
). – Ian Commented Jun 11, 2014 at 14:55 - watch this exception Cannot read property 'map' of null, seems the problem is that map function recieves null or undefined values from checkboxes – martinezjc Commented Jun 11, 2014 at 15:00
-
You should change this
for(var i=0;i<checkValues.length;i++) { var tot = tot + checkValues[i]; alert(tot); }
to this:var i,tot=0; for(i=0;i<checkValues.length;i++) { tot = tot + +checkValues[i]; alert(tot); }
. Javascript doesn't have block scope - so it's less confusing if you write it outside of thefor
block. Also, you should initializetot
to zero because in the first iterationtot
's value isundefined
– thedude Commented Jun 11, 2014 at 15:08
4 Answers
Reset to default 3Try to change:
var tot = tot + checkValues[i];
to this:
var tot = tot + +checkValues[i];
OR
change this:
return $(this).attr('prezzo');
to this:
return +$(this).attr('prezzo');
This will coerce the string to a number
Check this out:
var tot = 0;
for(var i=0;i<checkValues.length;i++) {
tot = tot + parseInt(checkValues[i]);
alert(tot);
}
You keep reinitializing tot (which isn't strictly speaking a problem in JS, but it's bad form), plus you're initializing it to undefined initially, since you're defining it as itself. Initialize it to 0 before the loop.
Javascript is a duck typed (loosely typed) language. If it thinks they're strings, it'll concat them. Try forcing them into the data type you need. If they're integers, use parseInt(varName) + parseInt(varName2).
http://www.w3schools./jsref/jsref_parseint.asp