Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

javascript - Opera - parseInt 1

Status
Not open for further replies.

tester321

Programmer
Mar 13, 2007
150
CA
Hi i read online that Opera does not support parseInt, is this true? and if so how would i caluclate as an interger this:

var totPeps = parseInt(wNumGuests.options[wNumGuests.selectedIndex].value) + parseInt(wNumChildren.options[wNumChildren.selectedIndex].value);

Thanks!
 
Hi

I included your code in a HTML page and it gives the same result as FireFox and Opera.
Code:
<html>
<body>
<form name="fo">
<select name="ng">
<option value="123" selected="selected">one two three</option>
</select>
<select name="nc">
<option value="45" selected="selected">four five</option>
</select>
<input type="button" value="calc()">
</form>
<script type="text/javascript">
var wNumGuests=document.fo.ng;
var wNumChildren=document.fo.nc;

[blue]var totPeps = parseInt(wNumGuests.options[wNumGuests.selectedIndex].value) + parseInt(wNumChildren.options[wNumChildren.selectedIndex].value);[/blue]

alert(totPeps)
</script>
</body>
</html>
No idea where you read that, but I would not read there anything.

Feherke.
 
That's not true at all that Opera does NOT support parseInt.

I will say this about your statement though. You may want to get in the habit of showing what base your parseInt will be in, it will prevent possible errors in the future. To do that, change your OP code to the following:

Code:
var totPeps = parseInt(wNumGuests.options[wNumGuests.selectedIndex].value[!], 10[/!]) + parseInt(wNumChildren.options[wNumChildren.selectedIndex].value[!], 10[/!]);


The , 10 tells the parseInt to parse as a base 10 number.

I've ran into 1 or 2 situations where the value does change without specifying base.

parseInt(8, 2) = 100
parseInt(8, 10) = 8

Just some friendly advice, use it if you will.


[monkey][snake] <.
 
Cheers monksnake, thanks for the expanding my brain!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top