0

How come the following happens in the console? or am i misusing the indexOf?

document.forms:

[
<form id=​"form-0" name=​"form-0">​…​</form>​
, 
<form id=​"form-1" name=​"form-1">​…​</form>​
, 
<form id=​"form-2" name=​"form-2">​…​</form>​
]

document.forms.indexOf["form-0"]:

TypeError: Cannot read property 'form-0' of undefined

5 Answers 5

1

Document.forms is a collection. If you want the number of a form - as indicated in your comments -, the question remains: at which moment do you want that number? Anyway, you could create an array of forms:

var allforms = document.getElementsByTagName('form'), 
    formsArray = [];
for (var i=0;i<allforms.length;i++){
   if (allforms[i].id.match(/\d+$/)){
       var indexval = parseInt(allforms[i].id.replace(/(.+)(\d+)$/,'$2'),10);
       formsArray[indexval] = allforms[i];
   }
}

Now you have an Array containing references to all forms, and for each form an index value that reflects the form number you gave it via it's id. So: formsArray[0] contains a reference to forms['form-0'], formsArray[1] to forms['form-1'] etc.

Sign up to request clarification or add additional context in comments.

Comments

1

You've used the wrong syntax... indexOf is a method, so it should use parenthesis, not brackets.

someString.indexOf("form-0")

With an object, you can simply request the object using brackets:

document.forms["form-0"]

2 Comments

Oh %$^&.. i should delete this question.. :) but while i'm at is there a way to get the number of the form?
What number? The index in the forms set? You can always loop over the forms.length and then see if the forms[j] iteration is the form you want.
1

document.forms is an HTMLCollection. It is not an array. As such, it does not have an indexOf method.

You can convert it using Array.prototype.slice.call:

var formsArr = Array.prototype.slice.call(document.forms);

Note, however, that indexOf is not universally supported. I'm not entirely sure what you're trying to do, however, so it may well not be the necessary approach anyway.

1 Comment

Thanx. actually i'm only trying to get the form's number, how can i do that? extract the number from document.forms[1]
0

If you're trying to get the element by its ID, just use the ID directly.

document.forms['form-0'];

If you want its index, it would seem that you already have it in the ID. Just remove the "form-" part.

var index = 'form-0'.replace('form-','');

Or if you have the element, do the same via its ID property:

var myform = document.forms['form-0'];
var index = myform.id.replace('form-','')

Comments

0

If you want to access the elements of a form using javascript, then you have to use this: document.myform.elements[i], where i is the position of the element. You may refer this Javascript Form Objects - Properties and Methods tutorial. indexof is a method of javascript string object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.