SiteExperts.com Logo Home | Community | Developer's Paradise
User Groups | Site Tools | Site Information | Search
 Main Menu
 Forums
SiteExperts.com Forums
All Discussions

SiteExperts Feedback
The Lounge
Dynamic HTML
Site Design/ Critiques
HTML and CSS
XML Technologies
The Wireless Internet
Internet Explorer
Microsoft .NET
The Server
Technical Support

Sponsored Links

User Groups : Forums : SiteExperts : Dynamic HTML :

Previous DiscussionNext Discussion
 form "action" attribute via javascript

I'm feeling a bit daft today. I have a form on the page and want to read its "action" attribute with javascript. But I also have an element in that form named action. When I try to read the "action" attribute of the form element, it gives me the value of the input element. What am I doing wrong here?

<form method="post" action="somewhere.php" name="form1">
<input type="hidden" name="action" value="Hello">
</form>

<script type="text/javascript">

alert(document.forms[0].action); // is an object

alert(document.forms[0].action.value); // alerts "Hello", not "somewhere.php"

alert(document.getElementsByTagName("FORM")[0].getAttribute("action")); // is the INPUT, not the FORM, even though i'm getting the attribute off the form object... hmm

</script>

 

Started By frumbert on Feb 23, 2006 at 10:03:00 PM

6 Response(s) | Reply

Earlier Replies | Replies 2 to 6 of 6 | Later Replies
saintjude on Feb 24, 2006 at 1:03:22 AM (# 2)

What am I doing wrong here?

Er... having a form element named "action". Change it. Even "Action" might do.


MHenke on Feb 24, 2006 at 1:58:17 AM (# 3)

Well, the approach using DOM-Methods (#3) is definitely the right one.
But it seems we've a clear IE-bug here (FF correctly returns "somewhere.php").

Use document.getElementsByTagName("FORM")[0].getAttributeNode("action").value); instead to persuade IE to actually take the attribute node.

Of course, renaming the input element should also work, but it's far less geeky, and since when we let the browser dictate how we name our elements?  8)


saintjude on Feb 24, 2006 at 4:06:09 AM (# 4)

1995 ?


MHenke on Feb 24, 2006 at 4:27:37 AM (# 5)

Roughly, yep.


frumbert on Feb 27, 2006 at 12:16:10 AM (# 6)

getAttributeNode("action").value

good one MHenke :-) Hadn't thought of that (obviously, otherwise I wouldn't have asked).

saintjude > Yeah I know, but it came up that I has access to the form element but the html containing the element named "action" was a ssi in the page and I couldn't change that include file! How come "worst case scenarios" always come up in my line of work?


Earlier Replies | Replies 2 to 6 of 6 | Later Replies

To respond to a discussion, you must first logon.

If you are not registered, please register yourself to become a member of the SiteExperts.community.

User Name
Password
Copyright 1997-2004 InsideDHTML.com, LLC. All rights reserved.