The object does not support the property or method "focus"

I am using a text box tagsinputon my website from project .

And I'm trying to set focus on the text box, but it does not work. This gives me the following error:

Unhandled exception at line 37, column 9 in
LOCALDOMAIN-LINK-REMOVED
0x800a01b6 - JavaScript runtime error: Object doesn't support property or method 'focus'.

line (37)to which this error relates is:

searchQueryTB.focus();

My full code is:

document.onload = FocusSearchQueryTextBox();

function FocusSearchQueryTextBox() {
    var searchQueryTB = document.getElementsByClassName("TBhandle0F7X");
    searchQueryTB.focus();
}

Markup:

<input type="text" value="" name="SearchQuery" id="tagsinput" class="TBhandle0F7X" />

So, I assume this may have something to do with the fact that maybe the text field is actually not proper textfieldin that it is the jQuery user interface alone or something that is used in the project tagsinput? Or maybe I'm wrong, but so far I have not had any luck on Google, and there seems to be nothing to do with this problem on the project website.

jQuery, , , . .

, ?

+5
4

getElementsByClassName DOM, , TBhandle0F7X

,

var searchQueryTB = document.getElementsByClassName("TBhandle0F7X")[0];
+7

, getElementsByClassName ( , , ), , , id document.getElementById().

, IE focus(), "" , . IE, .

, autofocus HTML ( , ).

+3

document.getElementsByClassName DOM, DOM element focus , . searchQueryTB, ( ), , , id document.getElementById?

, - ( , Internet Explorer 8+ ), querySelector:

var searchQueryTB = document.querySelector('.TBhandle0F7X'); // assuming only a single text box with this class
+1

getElementsByClassName, , searchQueryTB .

:

document.onload = FocusSearchQueryTextBox();

function FocusSearchQueryTextBox() {
    var searchQueryTBs = document.getElementsByClassName("TBhandle0F7X"),
        searchQueryTB = searchQueryTBs && searchQueryTBs[0];

    searchQueryTB && searchQueryTB.focus();
}
+1
source

All Articles