PDA

View Full Version : Why This Script is not working With IE7.



bhagwat
07-04-2008, 07:40 AM
Why This Script is not working With IE7. it is running in Firefox.


<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" language="javascript">
function sel(val)
{
var sMonth = document.getElementById('idMonthOption').value;
//alert(idMonthOption.value);
// alert(sMonth);
alert(val);
}
</script>
</head>

<body>
<form name="XX">
<select name="selectMonth" >
<option >Monat wählen</option>

<option value="2006-26" id="idMonthOption" onclick="sel('Hello')" > 2006-26 </option>
<option value="2006-27" id="idMonthOption" onclick="sel('Hello1')" > 2006-27 </option>
</select>
</form>
</body>
</html>

jscheuer1
07-04-2008, 08:00 AM
It shouldn't work in FF either, but FF is error correcting the code for you. In javascript only one unique id is allowed per page. The script has no way to know which element that this line:


var sMonth = document.getElementById('idMonthOption').value;

refers to.

So it is just a matter of FF skipping the error by simply picking the first one it finds, and IE barfing on the error. Either way, it is still an error and would need to be corrected.

If you remove the comments, you will see that FF always picks:


<option value="2006-26" id="idMonthOption" onclick="sel('Hello')" > 2006-26 </option>

when determining what:


var sMonth = document.getElementById('idMonthOption').value;

and:


idMonthOption.value

are, regardless of which one is clicked.