Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Focus function is not working in angular

Simple focus is not working in angular

<div class="search pull-right tab-{{ showDetails2 }}" data-ng-click="showDetails2 = !showDetails2; showDetails = false; showDetails1 = false;searchFocus();">

html

<input type="text" data-ng-model="model.fedSearchTerm"
                    placeholder="New Search" required class="span12 fedsearch-box" />

MY function

$scope.searchFocus = function() {
            $('.fedsearch-box').focus();
        };
like image 519
Prashobh Avatar asked Aug 30 '13 07:08

Prashobh


People also ask

What is the use of focus ()?

focus() method sets focus on the specified element, if it can be focused. The focused element is the element that will receive keyboard and similar events by default.

How do you know if an element is in focus angular?

hasFocus() : whether the document or any element inside the document has focus. document. activeElement : Property containing which element currently has focus.

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.

How do you set the focus element?

To set focus to an HTML form element, the focus() method of JavaScript can be used. To do so, call this method on an object of the element that is to be focused, as shown in the example. Example 1: The focus() method is set to the input tag when user clicks on Focus button.


1 Answers

I encountered the same issue. Solved it by enclosing the focus command inside $timeout. Make sure you pass the $timeout parameter in the .controller function.

$timeout(function() { $('.fedsearch-box').focus(); });
like image 163
Jason Jan Ngo Avatar answered Oct 30 '22 22:10

Jason Jan Ngo