<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Cache-control" content="no-cache">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="-1">
<Script language = JavaScript>
function addOptionList(selectbox,text,value )
{
var optn = document.createElement('OPTION');
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
function removeOptionList(listbox,i){
listbox.remove(i);
}
function addOption_list(fromvar,tovar){
for(i=fromvar.options.length-1;i>=0;i--) {
var userlist=fromvar;
if(fromvar[i].selected){
addOptionList(tovar, fromvar[i].value, fromvar[i].value);
removeOptionList(userlist,i);
}
}
}
</Script>
<table align='center'>
<tr>
<td ><select multiple name='userlist' id='userlist' >
<option value='aaa'>aaa</option>
<option value='bbb'>bbb</option>
</select></td>
<td align='center' valign='middle'>
<input value='-->'
onClick='addOption_list(userlist,pouser);' type='button'>
<br><input value='<--'
onClick='addOption_list(pouser,userlist);' type='button'></td>
<td><select multiple name='pouser' id='pouser'>
<option id='test' value='ccc'>ccc</option>
</select></td>
</tr>
</table>
</body>
</HTML>
I am using the code above to select a name from left box and move it to the right box. The code is working in IE with/without DOCTYPE. But when I use DOCTYPE, it stops working in Firfox. I have spent a lot of time on it, but still couldn't figure out the problem. Also, I am a novice in Javascript, so please explain me the problem with code below (when I am using DOCTYPE). Thanks in advance for your help!!
You're relying on elements with ids showing up as global properties on the window (e.g. userlist
). Firefox only does that in quirks mode, which is why the doctype matters.
document.getElementById("userlist")
Boris Zbarsky 2012-04-06 19:11
Your markup does not match the DOCTYPE. I.e. you are not using valid XHTML 1.0 markup.
Paste you code into the xhtml validator and it will show you what's wrong.