PDA

View Full Version : IE/Firefox javascript html list issue



Java Developer
12-12-2008, 09:07 AM
The following code snippet works in Firefox but not in IE. However, if I have the alert message at that particular location, it works in IE as well.

Am I missing something

/*
* This method selects all the items in the list if "All" (index position 0)
* is selected
*/
function selectAll(){
var combo = document.getElementById('item');
alert("If this alert is present it works in IE");
var val1 = combo.options[combo.selectedIndex].value;
for( var i = 0; i < combo.options.length; i++ ) {
if(val1 == 0){
combo.options[i].selected = true;
}
}
}

<s:select list="elements" name="item" headerKey="0" headerValue="ALL"
multiple="true" size="4" id="item" onclick="selectAll);"></s:select>

vwphillips
12-12-2008, 11:19 AM
tested in IE and FF


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script language="JavaScript" type="text/javascript">
/*<![CDATA[*/
/*
* This method selects all the items in the list if "All" (index position 0)
* is selected
*/
function selectAll(){
var combo = document.getElementById('item');
var val1 = combo.options[combo.selectedIndex].value;
alert("If this alert is present it works in IE");
for( var i = 0; i < combo.options.length; i++ ) {
if(val1 == '0'){
combo.options[i].selected = true;
}
}
}

/*]]>*/
</script></head>

<body>
<select list="elements" name="item" headerKey="0" headerValue="ALL"
multiple="true" size="4" id="item" onclick="selectAll();">
<option value="0" >11111111</option>
<option value="1" >11111111</option>
<option value="2" >11111111</option>
<option value="3" >11111111</option>
</select>

</body>

</html>

jscheuer1
12-12-2008, 06:35 PM
If you are going to use that DOCTYPE, use valid markup for it:


<body>
<div>
<select name="item"
multiple="multiple" size="4" id="item" onclick="selectAll();">
<option value="0" >11111111</option>
<option value="1" >11111111</option>
<option value="2" >11111111</option>
<option value="3" >11111111</option>
</select>
</div>

</body>

However, an HTML 4.01 strict DOCTYPE is preferred in almost all cases. HTML 4.01 transitional may be used if the markup cannot made to validate strict.

Java Developer
12-15-2008, 04:45 AM
Tried out the code that you send, it didn't work. Maybe because I want it to work in IE 6 and from what I see below the problem is resovled in IE 7.

I can't believe this... The solution is easier that it seems but the explanation a bit flakey....

"Cross browser compatibility issues. Changing onclick to onchange has resolved the problem."
http://www.webdeveloper.com/forum/showthread.php?p=960032#post960032

Thanks guys for your help.