Go4Expert (http://www.go4expert.com/)
-   JavaScript and AJAX (http://www.go4expert.com/forums/javascript/)
-   -   Html Dom (http://www.go4expert.com/forums/html-dom-t490/)

prashantSum 2Nov2005 15:44

Html Dom
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.

pradeep 3Nov2005 10:24

Re: Html Dom
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:


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.

prashantSum 3Nov2005 11:20

Re: Html Dom
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...

pradeep 3Nov2005 11:52

Re: Html Dom
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.


  function check(t) /* this is called onBlur */
      alert("You can't leave the field empty");
  /* 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.


prashantSum 3Nov2005 14:50

Re: Html Dom
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.

pradeep 3Nov2005 15:11

Re: Html Dom
Did you try my code suggestion?

prashantSum 3Nov2005 15:29

Re: Html Dom
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.

pradeep 3Nov2005 15:37

Re: Html Dom
Please copy-paste the form code into your reply with the Javascript.I'll check it.

All times are GMT +5.5. The time now is 10:42.