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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NaN in Internet Explorer but not in Firefox? 1

Status
Not open for further replies.

jfdabiri

MIS
Feb 27, 2007
282
US
hi,
i have this javascript function that works ok in firefox, but in i.e. it returns NaN. is there anyway to fix this?
thanks.
Code:
function changeit(me)
{
var amount;
var model;
var amtx;
model = me.value;
if (model == "x20") 
        {
          amtx = "100.00";      
        } 
amount = parseInt(amtx); 
}
thanks.
 
Yes. You need to supply the second parameter (the radix - or the base).
Code:
amount = parseInt(amtx[!], 10[/!]);

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
Perhaps it's because you're never returning the result?

Code:
function changeit(me) {
	var amount;
	var model;
	var amtx;
	model = me.value;

	if (model == 'x20') {
		amtx = '100.00';
	}
	amount = parseInt(amtx, 10);
	
	[!]return (amount);[/!]
}

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
i tried it. that's not it. i don't think you have to specify the radix. the default is 10. the strange thing is that in firefox it works, but not in internet explorer.
any other suggestions?
thanks.
 
i don't think you have to specify the radix. the default is 10.

Sorry, but that's wrong. You should ALWAYS specify the radix. 99 times out of 100 it won't matter, until you run into a situation like this and you end up stumped because your code looks perfect:
Code:
var a = "0123456789";
var b = parseInt(a);
var c = parseInt(a, 10);
alert("a: " + a + "\nb: " + b + "\nc: " + c);
By always specifying the radix you never have to waste time debugging a problem like this.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

Finally, <. is a good thing!
 
thanks,
it still doesn't work with the radix specified. here's my code. another problem is that the value of the field is undefined. i think that's the problem.
Code:
<html>
<head>
<title>Models</title>
</head>

<script>
function changeme(me)
{
var amount;
var model;
var amtx;
amount = window.document.getElementById("amt");

model = me.value;      

if (model == "A20" | model == "A21") 
        {
          amtx = "25.00"; 
        } 

amtz = parseInt(amtx);
alert (amtz);

alert (amtx);

amount.value = amtz;
}
</script>
<form name="frm1" action="isrtord.asp" method="post"> 


     Model:  
 
     <SELECT NAME="modl" id="modl" SIZE="1" onchange="return changeme(this)">
     
     <OPTION>Select A Model</option> 
     <OPTION>A20</option> 
     <OPTION>A21</option> 
     <OPTION>A22</option>  
     </SELECT>  

     <INPUT type="text" NAME="amt" id="amt" size=10></td> 
 
</form>  
 
</body>
</html>
 
i found the problem. since internet explorer is a little bit retarded and can't figure things out, this line:
var model = me.value;
should be changed to this:
var model = frm1.modl.options[frm1.modl.selectedIndex].text;

any suggestions on why it works in firefox and not in internet explorer?
thanks.
 
since internet explorer is a little bit retarded and can't figure things out

Sorry, but in this instance it wasn't IE's fault (for once). I'm really quite surprised that FF worked, because it's usually more strict than IE. Your original code was checking for the value of the selected option:
Code:
var model = me.[!]value[/!];

However, you haven't assigned any values to any of your options. Honestly, neither browser should have worked in this instance because you haven't provided enough information for them to understand. Had you built your dropdown correctly and assigned values to each option, your initial code would have worked just fine:
Code:
     <SELECT NAME="modl" id="modl" SIZE="1" onchange="return changeme(this)">
     
     <OPTION [!]value=""[/!]>Select A Model</option>
     <OPTION [!]value="A20"[/!]>A20</option>
     <OPTION [!]value="A21"[/!]>A21</option>
     <OPTION [!]value="A22"[/!]>A22</option>  
     </SELECT>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

Finally, <. is a good thing!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top