Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable in Javascript

Status
Not open for further replies.

milams

Programmer
May 28, 2004
32
US
This is driving me up the wall here!

I have this form where based on if a checkbox is selected the action file on the form will change. First can this be done and how do I write a javascript variable to change in the action="change variable" of the form properties? Say by default, the action is set to action="actionrma_confirm.php" but when I click on the check box the action file could maybe change to action="rma.php".

Can this be done in javascript?
 
Note: This is untested but Try something like this.
Code:
function chgAction(formObj) {
  if (document.getElementById('chkBox').checked)
    formObj.action = "somefile.htm";
  else
    formObj.action = "someotherfile.htm";
}

<form name="myForm" action="someotherfile.htm">
<input type="checkbox" id="chkBox" onchange="chgAction(myform);">
</form>
[code]

You could pass in the id of the chkBox too if you wanted to use the function for multiple checkboxes or even pass in the string to use for the action if you wanted.


[COLOR=blue]It's hard to think outside the box when I'm trapped in a cubicle.[/color]
 
Hey I'm sorry, but I'm new to Javascript so I don't know what some that stuff means like "formObj.action". I think I understand the function and what it's doing, but writting the result based on if the checkbox is check or not is where I get confused. Under action"someotherfile.htm", why wouldn't you put the variable there for it to change when the checkbox is checked or not. If i'm not making sense, I do apologize. I'm just a little confused.
 
The "onchange" event in the checkbox code (which I think might possibly need to be changed to "onclick", although I'm not 100% sure of this) calls a JavaScript function named "chgAction" when its value is changed (by clicking, etc).

The chgAction function detects whether the checkbox is checked or unchecked, and sets the form action to the appropriate value.

However, the "action" is still needed on the form for two reasons:

1. Validation, and
2. So that if the user doesn't touch the checkbox, a default action will still be present.

Hope this helps,
Dan

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Thanks BillyRayPreachersSon, I understand it now and it works. Thanks again.
 
The "onchange" event in the checkbox code (which I think might possibly need to be changed to "onclick", although I'm not 100% sure of this) calls a JavaScript function named "chgAction" when its value is changed (by clicking, etc).

Whoops! You were right, onchange did not work.
I was at the time working on a select box script and mental bleed over I guess.
To make things worse I made a type where the form name had capitalization but did not capitalize in the function call.
Oh well, I said it had not been tested. [rednose]


It's hard to think outside the box when I'm trapped in a cubicle.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top