Google Answers Logo
View Question
 
Q: Set form action property with Javascript in IE6 ( No Answer,   7 Comments )
Question  
Subject: Set form action property with Javascript in IE6
Category: Computers > Programming
Asked by: gansos-ga
List Price: $5.00
Posted: 18 Mar 2005 20:59 PST
Expires: 17 Apr 2005 21:59 PDT
Question ID: 497058
This is a Javascript question relating to IE6. In an javascript
function, when I try to set the action property of a form that has
another element named "action," the script returns an error in IE.
However, it works in Mozilla and other standards-compliant browsers.

I have a form that looks something like this: 

<form onSubmit="return onSubmitForm()" method="get" name="login" id="login"> 
<input type="hidden" name="action" value="login"> 
<input type="text" name="username"> 
</form> 


The javascript function it calls looks like this: 

function onSubmitForm() { 
document.login.action = "login.php" 
} 

I have oversimplified the code to focus on what I think is causing the
problem. What IE tries to do is set the <input type="hidden"
name="action" ...> element to "login.php." This, of course, results in
an error. It should set the form action to "login.php."

How can I rewrite my code so that IE correctly sets the form action,
and not the element named "action"? Changing name="action" is not an
option, due to the script that is processing the form. Lastly, the
solution must be standards-compliant.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Set form action property with Javascript in IE6
From: ullfindsmit-ga on 18 Mar 2005 22:29 PST
 
try

document.getElementById("login")).action = "login.php"
OR
document.getElementById("login")).getAttribute("action") = "login.php"

Alwayz
-Smit.
Subject: Re: Set form action property with Javascript in IE6
From: gansos-ga on 21 Mar 2005 00:12 PST
 
First suggestion doesn't work in IE6. Second suggestion doesn't work
in FireFox Mac or IE6. Thanks for the suggestions, though.

Maybe there's a workaround for IE6 that won't break more compliant browsers?
Subject: Re: Set form action property with Javascript in IE6
From: nilesh_p-ga on 26 Mar 2005 12:30 PST
 
GANSON,
  I think i have the right solution for you.

use this: document.login.action.value = "login.php"
insted of what you are currently have: document.login.action = "login.php"

This will replace the Value of "login" to "login.php" for <input
type="hidden" name="action" value="login">  so it will be

you may test this by useing the Alert function...

function onSubmitForm() { 

// sets hidden field value to login.php from login
document.login.action.value = "login.php"
// Alert window, to see if the value was set right. test msg.
alert (document.login.action.value);
} 

so you are right, just missing the .value at end :)

hope that helps in you web dev. task

btw check out my website www.define-web.com check out my portfolio :)
Subject: Re: Set form action property with Javascript in IE6
From: nilesh_p-ga on 26 Mar 2005 12:53 PST
 
woops, i misread you question,
Its going to be a tuff one, because <form> tag has a Action attribute
and you got a hidden field name as action.
Subject: Re: Set form action property with Javascript in IE6
From: nilesh_p-ga on 26 Mar 2005 13:25 PST
 
ok, got a idea, not sure if this will work with what you have in mind.

My idea is to use js add in the Hidden action text field after setting
the action of Login Form.

so, 1) set form action value to 'login.php'
2) use JS to add the Hidden action text field to form
3) submit and what not

here is the snippet for adding a field with JS...

<INPUT TYPE="button" VALUE="add field" ONCLICK="addField(this.form,
'hidden', 'action','login.php');">

<SCRIPT>
function addField (form, fieldType, fieldName, fieldValue) {
  if (document.getElementById) {
    var input = document.createElement('INPUT');
      if (document.all) { // what follows should work 
                          // with NN6 but doesn't in M14
        input.type = fieldType;
        input.name = fieldName;
        input.value = fieldValue;
      }
      else if (document.getElementById) { // so here is the
                                          // NN6 workaround
        input.setAttribute('type', fieldType);
        input.setAttribute('name', fieldName);
        input.setAttribute('value', fieldValue);
      }
    form.appendChild(input);
  }
}
</script>

i found the above snippet on net, its a idea like i said, not sure
what you are doing with the action text field, so maybe it will work
for you or maybe not.
Subject: Re: Set form action property with Javascript in IE6
From: nhaus-ga on 18 Apr 2005 16:44 PDT
 
<script>
function onSubmitForm() {
document.login.action.value = "login";
return true;
} 
</script>

<form onSubmit="return onSubmitForm()" action="login.php" method="get"
name="login" id="login">
<input type="hidden" name="action" value="login"> 
<input type="text" name="username"> 
</form> 

Adding "action" to the form element *and* then the value of hidden
action element seems to work. Unless I'm missing something?
Subject: Re: Set form action property with Javascript in IE6
From: gansos-ga on 18 Apr 2005 17:59 PDT
 
nhaus,

Your suggestion sets the hidden input element's value to "login." I
need to set the form action. The idea is that I need to dynamically
set the action of the form in the presence of a separate element with
the name "action." In reality, the script would set the action based
on some input from the user. I still haven't figured this out. It's a
tricky one.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy