2
<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.

3
  • may be its document.form1.d.value = mul; ? Commented Sep 30, 2010 at 19:16
  • 1
    How are you calling the function? What does your HTML look like? Can you edit your question and add these details? Commented Sep 30, 2010 at 19:17
  • what calls this function, and what is the HTML for the form? Commented Sep 30, 2010 at 19:22

3 Answers 3

3

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');
​

Working jsfiddle

Sign up to request clarification or add additional context in comments.

Comments

2

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/

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.