Newbie Question about IE

J

James

I have the follow html code and the jhavascrit works in firefox, netscape
and Mozilla but not Internet Explorer can someone please tell me why

<script type="text/javascript">
<!--
function button_pressed()
{
vehicle=document.forms[0].elements[0].value
price=document.forms[0].elements[1].value
amount=document.forms[0].elements[2].value
cash=document.forms[0].elements[3].checked
card=document.forms[0].elements[4].checked
cheque=document.forms[0].elements[5].checked
total=price*amount
if(cash==true)
{
total=total+5
}
if(card==true)
{
temp=total*.015
total=total+temp
}
if(cheque==true)
{
total=total+2
}
document.forms[0].elements[6].value=total
}
function change()
{
vehicle=document.forms[0].elements[0].value
price=document.forms[0].elements[1].value
amount=document.forms[0].elements[2].value
cash=document.forms[0].elements[3].checked
card=document.forms[0].elements[4].checked
cheque=document.forms[0].elements[5].checked
if(vehicle=="Porche 911 Turbo")
{
document.forms[0].elements[1].value=315000
}
if(vehicle=="Aston Martin DB9")
{
document.forms[0].elements[1].value=210000
}
if(vehicle=="Dodge Viper")
{
document.forms[0].elements[1].value=110000
}
if(vehicle=="McLaen F1")
{
document.forms[0].elements[1].value=1000000
}
}
//-->
</script>
<form action="">
<p>
Enter Product <select name="Product" onchange=change()>
<option>---</option>
<option>Porche 911 Turbo</option>
<option>Aston Martin DB9</option>
<option>Dodge Viper</option>
<option>McLaen F1</option>
</select><br/><br/>
Price <input type="text" name="price"/><br/><br/>
How many <input type="text" name="amount"/><br/><br/>
</p>
<h4>Payment</h4>
<p>
<input type="radio" value="cash" name="pay"/>Cash<br/>
<input type="radio" value="card" name="pay"/>Credit Card<br/>
<input type="radio" value="cheque" name="pay"/>Money Order<br/><br/><br/>
Total: <input type="text" name="Total"/><br/><br/>
<input type="button" value="Send" onclick="button_pressed()"/>
</p>
</form>
 
G

Gérard Talbot

James a écrit :
I have the follow html code and the jhavascrit works in firefox, netscape
and Mozilla but not Internet Explorer


What exactly does not work in Internet Explorer? What error message do
you get? What does the MS-script debugger say?

can someone please tell me why
<script type="text/javascript">
<!--
function button_pressed()
{
vehicle=document.forms[0].elements[0].value

1- If vehicle is to be a global variable, then best is to declare it
outside the function.

2- Personally, I always prefer to use the name attributes for forms and
form controls. It's a lot more intuitive, easier to understand, review
by others, maintain over time.

document.forms[0].elements[0] refers to nothing self-descriptive,
meaningful, intuitive. But document.forms["Invoice"].Product is more
descriptive, more meaningful, intuitive.
price=document.forms[0].elements[1].value

Same thing with price and all the other vairables.

amount=document.forms[0].elements[2].value
cash=document.forms[0].elements[3].checked
card=document.forms[0].elements[4].checked
cheque=document.forms[0].elements[5].checked

I think you need none of these declarations anyway since these variables
just store boolean values.
total=price*amount
if(cash==true)
{
total=total+5

[snipped]

total too.
cash=document.forms[0].elements[3].checked

Here, you're querying a particular radio button value.
card=document.forms[0].elements[4].checked

[snipped]

Here too. Maybe this is where the problem occurs in MSIE.
Can you post an url for that code? Can you upload this code on a page?
<form action="">
<p>
Enter Product <select name="Product" onchange=change()>

[snipped]

Enter Product <select name="Product" onchange="change();">

Gérard
 
W

web.dev

James said:
I have the follow html code and the jhavascrit works in firefox, netscape
and Mozilla but not Internet Explorer can someone please tell me why

<script type="text/javascript">
<!--
function button_pressed()
{
vehicle=document.forms[0].elements[0].value
price=document.forms[0].elements[1].value
amount=document.forms[0].elements[2].value
cash=document.forms[0].elements[3].checked
card=document.forms[0].elements[4].checked
cheque=document.forms[0].elements[5].checked
total=price*amount
if(cash==true)
{
total=total+5
}
if(card==true)
{
temp=total*.015
total=total+temp
}
if(cheque==true)
{
total=total+2
}
document.forms[0].elements[6].value=total
}
function change()
{
vehicle=document.forms[0].elements[0].value
price=document.forms[0].elements[1].value
amount=document.forms[0].elements[2].value
cash=document.forms[0].elements[3].checked
card=document.forms[0].elements[4].checked
cheque=document.forms[0].elements[5].checked
if(vehicle=="Porche 911 Turbo")
{
document.forms[0].elements[1].value=315000
}
if(vehicle=="Aston Martin DB9")
{
document.forms[0].elements[1].value=210000
}
if(vehicle=="Dodge Viper")
{
document.forms[0].elements[1].value=110000
}
if(vehicle=="McLaen F1")
{
document.forms[0].elements[1].value=1000000
}
}
//-->
</script>
<form action="">
<p>
Enter Product <select name="Product" onchange=change()>
<option>---</option>
<option>Porche 911 Turbo</option>
<option>Aston Martin DB9</option>
<option>Dodge Viper</option>
<option>McLaen F1</option>
</select><br/><br/>
Price <input type="text" name="price"/><br/><br/>
How many <input type="text" name="amount"/><br/><br/>
</p>
<h4>Payment</h4>
<p>
<input type="radio" value="cash" name="pay"/>Cash<br/>
<input type="radio" value="card" name="pay"/>Credit Card<br/>
<input type="radio" value="cheque" name="pay"/>Money Order<br/><br/><br/>
Total: <input type="text" name="Total"/><br/><br/>
<input type="button" value="Send" onclick="button_pressed()"/>
</p>
</form>

So the problem you're having is that IE is not displaying the price of
the car that's selected. The problem occurs where you have the line:

vehicle=document.forms[0].elements[0].value

in your function change(). It would be better practice if you used the
"value" attribute in the <options> tag, and obtained which option got
selected.
 
S

Stephen Chalmers

James said:
I have the follow html code and the jhavascrit works in firefox, netscape
and Mozilla but not Internet Explorer can someone please tell me why

You haven't supplied a 'value' parameter to any of the options in the
select, so I.E. reads the values as null.
Enter Product <select name="Product" onchange=change()>
<option>---</option>
<option>Porche 911 Turbo</option>
<option>Aston Martin DB9</option>
<option>Dodge Viper</option>
<option>McLaen F1</option>
</select>


If the values will always mirror the text exactly, you could refer to
the text property of each option:

vehicle=document.forms[0].elements[0].text;
 
R

RobG

Stephen said:
You haven't supplied a 'value' parameter to any of the options in the
select, so I.E. reads the values as null.

Just to explain a little further to the OP, according to the HTML spec,
if there is no value attribute then the value of the option is the content.

IE does exactly that if the form is submitted, but when using script the
literal content of the value attribute is returned, not the value of the
option.


[...]
vehicle=document.forms[0].elements[0].text;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,125
Latest member
VinayKumar Nevatia_
Top