Keywords:
firefox innerHTML form input field value text select textarea
Problem:
In IE (and therefore the GWT test shell running on windows) you can call innerHTML on a DOM object and where it contains HTTP form input fields you will see an up-to-date reflection of the inputs from the user.
On firefox you simply get a reflection of the DOM as it was originally served up to the user.
This is a problem if you want to move HTML around and not loose the inputs already made by the user.
Eg: On clicking the button in firefox you won't see the input entered in the text box.
<form action="" method="get">
<SPAN id="MyContent">
<input type="text" name="textField" value="" /><br/>
</SPAN>
</form>
<button onClick="window.alert(MyContent.innerHTML);">discover user input</button>
Solution:
Found the solution on
comp.lang.javascript - Firefox does not reflect selected option via innerHTML. I've extended the example code from this post to handle checkbox, radio and textarea ...
The idea is, every input field on the page must have an onBlur="updateDOM(this)" event handler, forcing the DOM to be updated and reflect the user's input.
<script type="text/javascript">
//
// Will be called by input fields when in 'update DOM' mode. This will
// make sure that changes to input fields in the form will be captured
// in the DOM - not necessary in IE but is required in Moz, etc as the DOM
// will otherwise reflect the page as it was initially.
//
// inputField : the input field that has just been tabbed out of (onBlur) OR the ID of the input field
function updateDOM(inputField) {
// if the inputField ID string has been passed in, get the inputField object
if (typeof inputField == "string") {
inputField = document.getElementById(inputField);
}
if (inputField.type == "select-one") {
for (var i=0; i<inputField.options.length; i++) {
if (i == inputField.selectedIndex) {
inputField.options[i].setAttribute("selected","selected");
} else {
inputField.options[i].removeAttribute("selected");
}
}
} else if (inputField.type == "select-multiple") {
for (var i=0; i<inputField.options.length; i++) {
if (inputField.options[i].selected) {
inputField.options[i].setAttribute("selected","selected");
} else {
inputField.options[i].removeAttribute("selected");
}
}
} else if (inputField.type == "text") {
inputField.setAttribute("value",inputField.value);
} else if (inputField.type == "textarea") {
var text = inputField.value;
inputField.innerHTML = text;
inputField.setAttribute("value", text);
} else if (inputField.type == "checkbox") {
if (inputField.checked) {
inputField.setAttribute("checked","checked");
} else {
inputField.removeAttribute("checked");
}
} else if (inputField.type == "radio") {
var radioNames = document.getElementsByName(inputField.name);
for(var i=0; i < radioNames.length; i++) {
if (radioNames[i].checked) {
radioNames[i].setAttribute("checked","checked");
} else {
radioNames[i].removeAttribute("checked");
}
}
}
}
</script>
<form action="" method="get">
<SPAN id="MyContent">
<input type="text" name="textField" value="" onBlur="updateDOM(this)"/><br/>
</SPAN>
</form>
<button onClick="window.alert(MyContent.innerHTML);">discover user input</button>
Notes:
It gets slightly trickier if you have input fields that don't get filled in by the user - eg a date picker dropdown, which will set the textbox with the date for the user, hence they never click in the box and trigger the 'onBlur'. In this case, you'd put the onBlur event on the date picker button
Eg:
<input type="text" name="dateField" id="dateField" value="" onBlur="updateDOM(this)"/>
<button id="myDatePicker" onClick="... do datepicking stuff ..." onBlur="updateDOM('dateField')">
... date picking image ...
</button>
Post updated (Thu, 25 Mar 2010): With thanks to the helpful commentors, the above script incorporates better handling for textArea & radio fields as well as an issue I came across for 'select-multiple' (was missing from the original script). It should work fine with the update via document or form approaches discussed in the comments - as opposed to the onBlur which continues to be good enough for my usage.
Post updated (Fri, 5 Apr 2013): Beware trying to set innerHTML text with newlines - IE will lose them. Work around is set the value attribute
afterwards and this will honour the newlines (and is meaningless for other browsers).