SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2002
    Location
    Asker, Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getElementsByTagName where input but not hidden

    I have a code that is supposed to select the first field in a login form. But sometimes there comes an extra hidden field with sissionID

    So I want to select the first input field, but not the hidden (it causes an error)
    My js code is
    Code:
     
    function focusFirst() {
      if (els = document.getElementsByTagName("input")) {
      els[0].focus();
      }
      }
      window.onload = function() {
      focusFirst();
    }
    The sessionID field is
    HTML Code:
    <input type="hidden" name="PHPSESSID" value="bbaed30be7cedcb03df00abe4a6076e7" />
    The whole form:
    HTML Code:
    <FORM name="user_auth" action="/" method="post">
    <input type="hidden" name="PHPSESSID" value="bbaed30be7cedcb03df00abe4a6076e7" />
    <LABEL for="username">Brukernavn:</LABEL><INPUT type="text" name="username" id="username" size=12 tabindex=0><br>
    <LABEL for="userpsw">Passord:</LABEL><INPUT type="password" name="userpsw" id="userpsw" size=12><br>
    <INPUT type="submit" value="Logg inn">
    </form>

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function focusFirst() {
      document.getElementById("username").focus();
    }
    Any reason you can't do it this way?

    If not, you'll have to iterate through the els[] collection, checking for the 'hidden' attribute.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2002
    Location
    Asker, Norway
    Posts
    125
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo
    Any reason you can't do it this way?
    Nope.
    I just didn't know I could do it that way

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, LOL.

    I thought maybe you were attempting to write an über-generic script that you wanted to reuse on several pages with different forms in them.
    Birnam wood is come to Dunsinane


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
  •