<script type="text/javascript">
function up(d,mul)
{
alert(d);
form1.d.value=mul;
}
</script>
up is a function name with which i am trying to update the value of field(field name=d). But its not working. plz somebody help me.
<script type="text/javascript">
function up(d,mul)
{
alert(d);
form1.d.value=mul;
}
</script>
up is a function name with which i am trying to update the value of field(field name=d). But its not working. plz somebody help me.
Hi Mohit, can you post your HTML code and the code piece that invoke this function. This way you can provides us more detailed information.
Well you pass d as parameter. So you either have to do (renaming it do fieldname):
function up(fieldname,mul)
{
document.form1[fieldname].value=mul;
}
and calling it with up('d', 'newValue'),
or let d be:
function up(mul)
{
document.form1.d.value=mul;
}
Not sure if you need document but I think you do.
See an example here: http://jsfiddle.net/8uyv8/
function up(d,mul) { alert(d); form1[d].value=mul; }
You can't use d literally here as it assumes you are looking for an element named "d". So you have to use d in a context where it will use it's value, in this case, an array index.
You can handle it like so:
The HTML:
<form method='post' action='doesnt_matter'>
<input type='text' name='field1' />
<input type='text' name='field2' />
</form>
The JavaScript:
form = document.forms[0];
function up(d,mul)
{
alert(d);
form[d].value=mul;
}
up('field1','Hello field 1');
up('field2','Hello field 2');