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

Thread: Can I reference to a form object by its html-tag name ?

  1. #1
    Join Date
    Mar 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Can I reference to a form object by its html-tag name ?

    A simplified situation, when I reference the textarea by id from the onClick() event:
    Code:
    <form name="news"  method="post" action="">
    <textarea name="msg" cols="60" rows="15" id="msg">
    <input type="button" name="b1" id="b1" value=" U " onclick="doSomething(this.form.msg)"
    </form>
    The doSomething() function is in an outer file.
    I want my code as much as portable as it can, so I would like to refence the textarea by html-tag name: eg this.form.textarea . (because I think, in my case, 1 form has only 1 textarea). So when I copy this code to another html file, I don't have to rewrite the parameter (you may say, that it's not a problem, but when I have lot of button, it is.)

    Or is there any simple solution for this ?

    Thanks

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

    Default

    A much better idea (and a much more portable one) would be to pass the textarea as another argument to doSomething().
    However, yes, you can reference the textarea as this.form.getElementsByTagName("textarea")[0].
    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
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by chros
    A simplified situation, when I reference the textarea by id [...]
    If a form control is placed within a form element, it's better to use the name attribute value; more portable. The id attribute is of lesser value for identification in this situation, though by all means include one for other purposes (like style sheets).

    Mike

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In your example the textarea and the input tags are both invalid. The textarea has no closing tag and the input tag is malformed, no closing '>'. Try this:

    HTML Code:
    <form name="news"  method="post" action="">
    <textarea name="msg" cols="60" rows="15" id="msg">Hey</textarea>
    <input type="button" name="b1" id="b1" value=" U " onclick="alert(this.form.elements[0].value)">
    </form>
    This depends upon the textarea being the first element in the form, if your function looks like so or has similar language:

    Code:
    <script type="text/javascript">
    function alertArea(form){
    for (var i_tem = 0; i_tem < form.elements.length; i_tem++)
    if (form.elements[i_tem].tagName.toLowerCase()=='textarea'){
    alert (form.elements[i_tem].value)
    break;
    }
    }
    </script>
    It will find the first textarea in the form. Your markup could be like so:

    HTML Code:
    <form name="news"  method="post" action="">
    <textarea name="msg" cols="60" rows="15" id="msg">Hey</textarea>
    <input type="button" name="b1" id="b1" value=" U " onclick="alertArea(this.form)">
    </form>
    - John
    ________________________

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

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

    Default

    Quote Originally Posted by John
    Code:
    <script type="text/javascript">
      function alertArea(form) {
        for (var i_tem = 0; i_tem < form.elements.length; i_tem++)
          if (form.elements[i_tem].tagName.toLowerCase()=='textarea') {
            alert (form.elements[i_tem].value)
            break;
          }
      }
    </script>
    However, using the native getElementsByTagName() method is more efficient.
    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!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey
    However, using the native getElementsByTagName() method is more efficient.
    Not as x-browser.
    - John
    ________________________

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

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

    Default

    True.
    Code:
    <script type="text/javascript">
      function alertArea(form) {
        if(typeof form.getElementsByTagName != "undefined")
          alert(form.getElementsByTagName("textarea")[0].value);
        else for (var i_tem = 0; i_tem < form.elements.length; i_tem++)
          if (form.elements[i_tem].tagName.toLowerCase()=='textarea') {
            alert (form.elements[i_tem].value);
            break;
          }
      }
    </script>
    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!

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey
    True.
    Code:
    <script type="text/javascript">
      function alertArea(form) {
        if(typeof form.getElementsByTagName != "undefined")
          alert(form.getElementsByTagName("textarea")[0].value);
        else for (var i_tem = 0; i_tem < form.elements.length; i_tem++)
          if (form.elements[i_tem].tagName.toLowerCase()=='textarea') {
            alert (form.elements[i_tem].value);
            break;
          }
      }
    </script>
    That's good, Twey - a little complicated as an example perhaps but, much more efficient looking and probably is more efficient. I am no expert on code efficiency though. I was a little worried that the OP might not be able to follow my version, hopefully, with the full discussion of this thread, what is being done will all be clear.
    - John
    ________________________

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

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

    Default

    Quote Originally Posted by John
    I am no expert on code efficiency though.
    Nor am I, when it comes to Javascript However, a native implementation will always be faster than one written in Javascript, for the simple reason that it doesn't have to be interpreted.
    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!

  10. #10
    Join Date
    Mar 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the answers, I'll reply them in the afternoon. (It's early morning here )

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
  •