0

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 &euro;</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 &euro;</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?

4
  • 1
    var tot = tot + checkValues[i]; - the tot on right hand side is not initialized . hence the result. Put the declaration outside the loop var tot=0; 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 (or undefined). 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 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 the for block. Also, you should initialize tot to zero because in the first iteration tot's value is undefined Commented Jun 11, 2014 at 15:08

4 Answers 4

3

Try 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

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

Comments

2

Check this out:

var tot = 0;
for(var i=0;i<checkValues.length;i++) {
    tot = tot + parseInt(checkValues[i]);
    alert(tot);
}

2 Comments

I fix it following your "tot = tot + parseInt(checkValues[i]);" and the advice that gave me raligan! I'm very happy I fixed my problem!! THANK YOU :DDD PS=I tried to use parseInt but it didn't function... maybe I was doing it wrong!
@GiacomoCerquone Alternatively, you could use for(var i=0, tot=0;i<checkValues.length;i++) {
1

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.

2 Comments

While this fixes the undefined problem, it will still do string concatentation, not addition
it's anyway an error from me. Thank you, I'm fixing it :)
1

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.com/jsref/jsref_parseint.asp

1 Comment

If on of the variables is "undefined," parseInt won't be able to parse it because undefined is not a number. Check to see if your values are numbers by using IsNaN(var1) before parsing.

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.