最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Object doesn't support property or method "focus" - Stack Overflow

programmeradmin5浏览0评论

I am using the tagsinput text field on my website from this project.

And I am trying to set focus to the text field but it isn't working. It is giving 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'.

The line (37) that this error is referring to 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'm assuming it may have something to do with the fact that maybe the text field isn't actually a proper textfield in that it's a jQuery UI one or something that's used by tagsinput project? Or maybe I'm wrong, but I haven't had any luck thus far on Google and there doesn't appear to be anything related to this issue on the project's website.

I've also tried this in jQuery, but as expected, it didn't work either. Same error.

Any suggestions as to what is wrong or how I could fix this?

I am using the tagsinput text field on my website from this project.

And I am trying to set focus to the text field but it isn't working. It is giving 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'.

The line (37) that this error is referring to 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'm assuming it may have something to do with the fact that maybe the text field isn't actually a proper textfield in that it's a jQuery UI one or something that's used by tagsinput project? Or maybe I'm wrong, but I haven't had any luck thus far on Google and there doesn't appear to be anything related to this issue on the project's website.

I've also tried this in jQuery, but as expected, it didn't work either. Same error.

Any suggestions as to what is wrong or how I could fix this?

Share Improve this question edited Sep 28, 2016 at 5:42 Aᴄʜᴇʀᴏɴғᴀɪʟ 3,0574 gold badges26 silver badges50 bronze badges asked Jun 28, 2012 at 4:39 ArrowArrow 2,9048 gold badges41 silver badges61 bronze badges 7
  • 2 searchQueryTB is an array.. – UltraInstinct Commented Jun 28, 2012 at 4:49
  • What browser are you using? From your comment in the answers below, it seems you might be using one that doesn't support focus natively. – mgibsonbr Commented Jun 28, 2012 at 4:57
  • I am using Internet Explorer 10 in Windows 8 Release Preview. – Arrow Commented Jun 28, 2012 at 4:59
  • 2 Unfortunatly I don't have a similar environment to test it, so I can't be sure, but I'm still betting on a bug or browser quirk. I tested "tagsinput" for the native focus on Firefox and Chrome and it didn't raise any exception. Could you please run your code in another browser and see what happens? – mgibsonbr Commented Jun 28, 2012 at 5:10
  • Please note: I have also tried the above with both Class and Id (getElementsByClassName() and getElementById()) – Arrow Commented Jun 28, 2012 at 5:10
 |  Show 2 more comments

4 Answers 4

Reset to default 8

getElementsByClassName returns a collection of DOM elements not an element itself even if there is only one element with class TBhandle0F7X

You probably want

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

In addition to the issue that getElementsByClassName returns an array (and you would thus need an index), there’s the problem that this method is not supported by all browsers, so it is better to use an id attribute and document.getElementById().

But the problem remains that IE has issues with focus(), a “lazy” implementation, see focus doesn't work in IE which contains some solutions.

Additionally, you could consider using the autofocus attribute in HTML markup (supported by modern browsers even when scripting is disabled).

document.getElementsByClassName returns a collection of DOM elements so you cannot invoke the DOM element focus method on the variable you assigned that collection to. You can assign the element at the first index of the collection to searchQueryTB, but it seems to me (and this could be a completely wrong assumption on my part) that there is only a single text box that you're going to be focusing on so why not just give it an id attribute and use document.getElementById?

If you expect your users to have modern web browsers (basically, Internet Explorer 8+ or any other browser) you can use the querySelector method:

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

use getElementsByClassName, the result is an array-like, so in your code searchQueryTB is an array.

solution:

document.onload = FocusSearchQueryTextBox();

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

    searchQueryTB && searchQueryTB.focus();
}
发布评论

评论列表(0)

  1. 暂无评论