SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get Element Length

    I have a text field with name "title"

    I have this javascript to test it

    Code:
    <script>
    function checkelement(element_name) {
    var tlength = document.getElementsByName(element_name).length;
    alert("The length is "+ tlength +"");
    return false;
    }
    </script>
    on my form I have
    onSubmit="return checkelement(title)"

    All I get is an alert saying '0' every time I submit.

    Help?

    Thanks
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try single quotes around 'title' because element_name should be a string:
    Code:
    onsubmit="return checkelement('title')"

  3. #3
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After looking at your other thread I think I misunderstood your question here.
    I think you need something like this:
    Code:
    document.getElementsByName(element_name)[0].value.length;
    That is, if you really want to use getElementsByName().

  4. #4
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, i got this working perfect.

    but i need a function around it

    Code:
    if(document.getElementsByName(element_name)[0].value.length > 3) {
    			return true;
    		} else {
    			alert("Please make sure the Title field was properly completed.");
    			return false;
    		}
    I have radio buttons named 'use'. the values are either "y" or "n". Since the form is dynamic, they may not even exist.

    if the use radio is checked on "y", I want the entire function to skip the function above and just return true. If not (checked "n" or the radio does not exists), I want the function to kick in above.

    How can I do this?

    Thanks
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.

  5. #5
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Ryan,

    There are several different ways to do this but without seeing your html I will just show one of the ways it could be done:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Template</title>
    <script type='text/javascript'>
    window.onload = function()
    {
      document.forms['form1'].onsubmit = f1OnSubmit;
    }
    function f1OnSubmit()
    {
      var i, e = this.elements['use'];
      if (e) {
        for (i = 0; i < e.length; ++i) {
          if (e[i].checked && e[i].value == 'y') {
            return true;
          }
        }
      }
      var t = this.elements['title'];
      if (t && t.value.length <= 3) {
        alert("Please make sure the Title field was properly completed.");
        return false;
      }
      return true;
    }
    </script>
    </head>
    <body>
    <form name='form1'>
    <p><input type='text' name='title'></p>
    <p><input type='radio' name='use' value='y'> Yes</p>
    <p><input type='radio' name='use' value='n' checked='true'> No</p>
    <p><input type='submit' value='OK'></p>
    </form>
    </body>
    </html>

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2004
    Location
    Newport Beach
    Posts
    1,761
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Worked, I owe you a good spooning.

    Thanks
    Ryan
    Upcoming Movies - Movie News. Updated Daily.
    Movie Trailers - Awesome trailer site. Nuff said.


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
  •