Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: change form.action while having another element named "action"

  1. #1
    Join Date
    Oct 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow change form.action while having another element named "action"

    i need to change the action of my form like so:
    document.regform.action = 'preregister.php';

    the problem is, i have to have this also:
    <input type="hidden" name="action" value="createnew">

    it works fine in Firefox, but in IE, i get an error that the object doesn't support that method. i guess it's trying to change the hidden input (code works fine without that input).

    surely there's a way to do this?
    i've tried other means of changing the action, like:
    document.getElementById('blah').action
    or
    document.forms[0].action

    but same result with all.

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I think you may have to resort to document.forms.regform.setAttribute('action', 'preregister.php'). The easiest solution, though, is of course to rename the "action" element.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,127
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default

    That seems reasonable, Twey. In testing here, it didn't pan out though. This little demo did:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function chng_a(el){
    for (var i = 0, a=[], p=el.form.elements; i < p.length; i++)
    if(p[i].name&&p[i].name.toLowerCase()=='action')
    a.push(p[i].parentNode.removeChild(p[i]));
    el.form.action='http://www.google.com';
    for (var i = 0; i < a.length; i++)
    el.form.appendChild(a[i]);
    }
    </script>
    </head>
    <body>
    <form action="#" name="regform">
    <div><input type="hidden" name="action" value="createnew">
    <input type="button" onclick="chng_a(this);" value="Change Action"><br>
    <input type="submit" value="Go!"></div>
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    However, that a) is a Horribly Ugly Hack©, and b) rearranges the form elements, something that's likely to have a less-than-pleasant effect on presentation unless every element is absolutely positioned (and if they all are, you've worse problems to worry about).
    In testing here, it didn't pan out though.
    You've been here long enough to know that this isn't very helpful in diagnosing a problem. Can you say what went wrong?
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,127
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey View Post
    However, that a) is a Horribly Ugly Hack©, and b) rearranges the form elements, something that's likely to have a less-than-pleasant effect on presentation unless every element is absolutely positioned (and if they all are, you've worse problems to worry about).
    Good point about the rearrangement, but with what was put forward (a hidden element), it should be no problem as long as the form's elements aren't later referenced by number, something easy to avoid. However, if the layout must be preserved, the array could become multidimensional and store a reference to the element's position, so that it could be reinserted there.

    I'm not sure how it is a hack though.

    Quote Originally Posted by Twey View Post
    You've been here long enough to know that this isn't very helpful in diagnosing a problem. Can you say what went wrong?
    I thought that should have been obvious. To wit, the setAttribute method, although specifically designed to seek out an attribute and not an element, nevertheless found only the element in IE 7 (and in Opera 9.01).

    As a side note, I tried (and discarded):

    Code:
    for (var i in el.form)
    if(i=='action')
    el.form[i]='new_url';
    Because only IE saw two i=='action' members of the form, but even so, when it went to access either of them, accessed only the element. Opera and FF saw and accessed only one, the element.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I'm not sure how it is a hack though.
    Removing every element with a name of "action", accessing the property, then putting them all back in?
    Quote Originally Posted by the Jargon File
    1. n. Originally, a quick job that produces what is needed, but not well.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,127
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default

    There is only one.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    In this case. It still requires three lines of code and a considerably greater amount of resources in order to perform a single assignment, as well as being a workaround that shouldn't be necessary, for an issue that shouldn't exist (it being the result of a misguided "shortcut").
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,127
    Thanks
    44
    Thanked 3,228 Times in 3,189 Posts
    Blog Entries
    12

    Default

    Yes, the name 'action' should be avoided in the first place, even if that means changing other code extensively. Here's a better method than removing the node though:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    function chng_a(el){
    for (var i = 0, a=[], p=el.form.elements; i < p.length; i++)
    if(p[i].name&&p[i].name.toLowerCase()=='action'){
    a.push([p[i], p[i].name]);
    p[i].removeAttribute('name', 0);
    }
    el.form.action='http://www.google.com';
    for (var i = 0; i < a.length; i++)
    a[i][0].setAttribute('name', a[i][1], 0);
    }
    </script>
    </head>
    <body>
    <form action="#" name="regform">
    <div><input type="hidden" name="action" value="createnew">
    <input type="button" onclick="chng_a(this);" value="Change Action"><br>
    <input type="submit" value="Go!"></div>
    </form>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Somewhat better, yes.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •