Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

document.getElementById().focus() returning undefined

I have the following HTML:

<input type="text" id="endDateEditBox" value="" style="margin-left:5px; height:18px; width:70px; vertical-align:middle;" onchange="validateDate('endDateEditBox', $(dateFormatErrorString).value)">

And I am trying to make this focus using the following Javascript:

document.getElementById("endDateEditBox").focus();

Which chrome console and Firebug both return as undefined. I feel like I am missing something very obvious here, can anyone help?

like image 290
Fraser Avatar asked Feb 03 '12 11:02

Fraser


People also ask

Why is getElementById undefined?

This error TypeError: document. getelementbyid(...) is null would seem to indicate that there is no such element with an ID passed to getElementById() exist. This can happen if the JavaScript code is executed before the page is fully loaded, so its not able to find the element.

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.

Why is getElementById returning NULL?

If there is no element with the given id , this function returns null . Note that the id parameter is case-sensitive, so document.getElementById("Main") will return null instead of the element <div id="main"> because "M" and "m" are different for the purposes of this method.


1 Answers

The focus() method does not return a value. It sets the focus on the matched input, and then returns undefined...

like image 160
Matt Avatar answered Oct 18 '22 14:10

Matt