Hi,
I want to load the multiplication result of 1st and 2nd input text in the 3rd input text before clicking the submit portion of the form (all the values in the input texts will be numeric)
I tried with this code but something is worng here
<script type="text/javascript">
function myfunc() {
var a = document.my.n1.value;
var b = document.my.n2.value;
var c = a*b;
return c;
}
</script>
<form name="my" method="post">
<input type="text" name="n1"/>
<input type="text" name="n2"/>
<input type="text" onChange="myfunc()"/>
<input type="submit" />
</form>
Kalon, many thanks for your code. Your code is working great. But as you entered fieldset tag in the form,the 2 input boxes look different from the 3rd one. Is there anyway, with which all input boxes will look same like a simple form but also will work correctly like your code?
as pmw57 said, the only purpose of the fieldset in my demo code is to make it easier to get the 2 input element objects. I could have just as easily used a div.
normally, unless I had to wrap ths inputs for another purpose I would have just given them a class name and then used a customised getElementsByClassName() function to get the 2 input elements. but that means a little more code and I wanted to keep the demo as simple as possible.
function getElementsByClassName(oElm, strTagName, strClassName) {
var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
var arrReturnElements = new Array();
strClassName = strClassName.replace(/\\-/g, "\\\\-");
var oRegExp = new RegExp("(^|\\\\s)" + strClassName + "([\\\\s|$](file:///s:$))");
var oElement;
for(var i=0; i<arrElements.length; i++){
oElement = arrElements[i];
if(oRegExp.test(oElement.className)){
arrReturnElements.push(oElement);
}
}
return (arrReturnElements)
}
and pmw57 showed another way of getting the 2 inputs if you don’t wrap them. quite often there is more than 1 way to do things.