Html Dom

Discussion in 'JavaScript and AJAX' started by prashantSum, Nov 2, 2005.

  1. prashantSum

    prashantSum New Member

    Joined:
    Oct 24, 2005
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    hi,
    using HTML DOM api can we know the number text boxes in the current page or number of text boxes in a <form> tag.

    I have seen 'document.anchors.length', but what I want is number of text boxes is there something like this 'document.text.length'.

    and further can we get text boxes of document or form as an array, so that I can manipulate those objects.
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    You can collect all the elements of a form into an Object, through which you can iterate to do whatever you want to.
    The forms collection:

    Code:
    var oForm = document.forms[0]; 
      //holds the reference to the first form in the documents,  
     //you can access the other forms by changing 
     //the subscript
      
      var iLength = oForm.elements.length; //get the number of form elements
      
      //if you want to find the no. of text boxes in a form, here's how you do it.
      
      var oElements = oForm.elements; //get all the elements of the for to oElements
      var iTextCount = 0; //to store the no. of text boxes
      
      for(var i in oElements) //this is the JavaScript equivalent of Perl foreach
      {
         if(oElements[i].type == "text")
      	  iTextCount += 1;
      }
      
      //Now iTextCount stores the no. of text boxes in the form.
      //You can do a lot of things with forms in this way.

    Hope this was helpful to you. Drop a query if you wanna know anything else.
     
  3. prashantSum

    prashantSum New Member

    Joined:
    Oct 24, 2005
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    thanks for the reply pradeep

    actually my problem is : there are 3 text boxes(with tabindexes given in serial) for which I need to do validate (which should not be empty), so I have written a function to check whether the text box is empty or not, and I am calling this function on 'onblur' event of text box.
    now the problem is if I press a tab in first text box the focus is going to 2nd text box, I don't know whether it going before executing the check function or after executing it, so first time it will show alert message and as the first box is empty I change the focus to first box, now as the focus is shifting from second text box to first text box the second text box onblur event is getting fired as it will be empty and like this it is going into infinite loop...

    one more problem with the 'onblur' event is even if I am going to some other window that event is firing and executing the check function...

    I am trying find a solution.........thinking so many logics.....

    if u have please tell me..

    one more thing how can we know whether a text box is in focus or not...
    can we do something like this textbox.focus == true...
     
    Last edited: Nov 3, 2005
  4. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Hi,
    I think you should validate the text boxes onSubmit, not onBlur.If your want to focus a field then this is how you go about it.

    Code:
      function check(t) /* this is called onBlur */
       {
         if(t.value.length<1)
         {
           alert("You can't leave the field empty");
           t.focus();
         }
       }
       /* t is the reference to the source element, so when calling the function 
       call it like this onBlur="check(this);"
       */
       
    Check out this thread http://www.go4expert.com/showthread.php?t=452 for more help on form validation with javascript.

    http://spradeep.blogspot.com
     
  5. prashantSum

    prashantSum New Member

    Joined:
    Oct 24, 2005
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    yes generally we validate it only on submit,

    but I have requirement to validate each text box when ever the user leaves the text box.
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Did you try my code suggestion?
     
  7. prashantSum

    prashantSum New Member

    Joined:
    Oct 24, 2005
    Messages:
    57
    Likes Received:
    0
    Trophy Points:
    0
    hi pradeep,
    initially the contains the some syntax errors like some braces are missing.

    I corrected and tried to run it, where in I have two text fields and one list box, but it's only validating for first field and not checking or validating 2nd text box and 3rd list box..

    and more over this not what I want,

    what I want is it should check immediately whenever the user leaves the text box.
     
  8. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Please copy-paste the form code into your reply with the Javascript.I'll check it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice