...

View Full Version : Setting value of Button with DOM



Mike777077
04-17-2009, 07:54 PM
I am trying to set the value of a regular form input button using the Document Object Model (DOM), however, I am not able to do so in fcirefox, the following code does not work in Firefox but works fine in Internet Explorer.

Please provide some input

<!--- Javascript here --->
function SetValue() {
document.getElmentById("OneButton").value = "New Value";
}

<!-- html here --->
<form method="post">
<input type="button" name="OneButton" id="OneButton" value="First Value" onclick="SetValue();">
</form>

bdl
04-17-2009, 08:50 PM
Note that the method is getElementById, not getElmentById.

Also, do you have the JavaScript in your markup body (i.e you're using HTML comment tags) instead of inside a SCRIPT tag?

Two other things to consider: you should make sure that the DOM functions are set after the document loads, i.e. using a window.onload event handler, or pushing the JS code down below the markup at the bottom of the body tag. The other thing is, you should probably reference the 'value' attribute with element.setAttribute("value","whatever"), e.g.


<!-- html here --->
<form method="post">
<input type="button" name="OneButton" id="OneButton" value="First Value" onclick="SetValue();">
</form>

<script>
function SetValue() {
document.getElementById("OneButton").setAttribute("value","New Value");
return false;
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum