Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript focus() and select() quirk

Im working on a form and getting null or not an object errors in ie.

<form action="#" method="post" name="adv_search">

<input class="inputbox" type="text" name="keyword1" value="none" id="keyword1"/>
</form>

<script>
document.adv_search.keyword1.focus();
document.adv_search.keyword1.select();
</script>

//whereas if I use

<script>
var key1 = document.getElementById('keyword1');
   key1.focus();
   key1.select();
</script>

//everything is fine

i would like to understand why. i would like it to work without having the id tag for the input field

thanks in advance


shouldnt the document.formname.fieldname.focus(); and document.formname.fieldname.select(); work?

like image 465
chris Avatar asked Apr 06 '09 12:04

chris


People also ask

What is focus () in JavaScript?

JavaScript | Focus() JavaScript focus method is used to give focus to a html element. It sets the element as the active element in the current document. It can be applied to one html element at a single time in a current document. The element can either be a button or a text field or a window etc.

Can a div have focus?

When a div with a scroll bar is focused, the arrow keys will scroll its content (instead of the content of other elements such as body ).


2 Answers

Your particular example works for me, but if I add another field with the same name:

<input type="text" name="keyword1" />
<input type="text" name="keyword1" />

Then document.adv_search.keyword1.focus() will fail with the error you specify.

The reason is that:

document.adv_search.keyword1

is a shortcut for this syntax (which goes back to DOM Level 0 and the Netscape 2 days!):

document.forms.adv_search.elements.keyword1

(Incidentally, it is better to use this full syntax, instead of relying on the behaviour of the ‘document’ and ‘form’ objects being indexed on names: if a new method is added to HTMLDocument or HTMLFormElement, that might clash with the control name you are using. This is less of an issue when you use the document.forms or form.elements collections. Also, IE mistakenly dumps all names and ids into ‘document’, so if you've got an element with id="adv_search" in addition to the form with that as a name, document.adv_search will return the wrong one.)

Anyway, the DOM Level 0 scripting methods behave slightly curiously when you access an element by name like this. If there is a single matching element, you'll get that one as a standalone object. If, on the other hand, there are more than one, you'll get a list of objects. You can't call focus() or select() on an array-like list, which is why the error appears; you'd have to do something like keyword1[0].focus() when the list was returned.

So you have to decide whether you're going to be using old-school DOM Level 0 methods to access your form controls — in which case you're going to have to cope with sniffing for single-or-multiple-controls — or move to the ID-based methods introduced by ‘DOM Level 1’:

document.getElementById('keyword1').focus();

The ID-based methods are generally a bit more typing (in the script and to add ‘id’s to all elements you wish to access this way, if they don't already have them), but they are simple and unambiguous. (Also you can then drop the name on the <form> itself.)

like image 80
bobince Avatar answered Oct 10 '22 17:10

bobince


The ID approach really is best but if you want to go by name, use getElementsByName.

In this case, it might look like this:

<script>
   // retrieves array of objects with the name 'keyword1' 
   // and takes the first one
   var key1 = document.getElementsByName('keyword1')[0]; 
   key1.focus();
   key1.select();
</script>
like image 30
Michael Haren Avatar answered Oct 10 '22 16:10

Michael Haren