Jump to content

onchange value returning 'object HtmlFormElement'


dfin

Recommended Posts

I have a form where I need the text value to be passed to another form element later on.

 

Right now, it have

<form method='post' action='https://secure.autho...ay/transact.dll' >
<input type='text' name='amount' value='50.00' />
Change: <input type="checkbox" name="chg" onclick="myF(this.form)">
<script type="text/javascript">
function myF(val) {
var num = val;
var n = num.toString();
alert("You have now changed the amount to pay: " + n );
}
</script>
<input type='text' name='x_amount' value='<?php echo $_REQUEST['num'] ?>' />
<input type='Submit' value='Pay Now'>
</form>

It will then return the Alert box & in place of + n, it says object HtmlFormElement.

 

I tried doing a document.getElementById(this.form), but I think that gave me a null.

 

I know I will later need to do a validation to make sure it is a number & 2 decimals, but for now I need my alert & variable to be passed right.

 

I've tried  + n, + val, had it to where it didn't create the toString...nothing works

A few things.

 

1) you can't use getElementById() unless there is actually something with the ID. You're not using ANY ID's on any of your form elements so it won't work.

2) onclick="myF(this.form)" You're sending the ENTIRE form to myF(). That's why you're getting an "object" in the alert()

 

Try changing these lines:

//give this form field an ID of 'amount'
<input type='text' name='amount' value='50.00' id="amount" />
 
//no need to pass anything to the myF() function
Change: <input type="checkbox" name="chg" onclick="myF()">
 
<script type="text/javascript">
function myF() {
    //get value from the form field that has the id of 'amount'
    var amount = document.getElementById('amount').value;
    alert("You have now changed the amount to pay: " + amount );  //alerts 50.00 unless value changed
}
</script>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.