Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autofocus on the disabled field that would be enabled after certain conditions

I have two input fields as shown below in the pic.

Two Input Fields

I am trying to focus on the next field which is disabled now but after entering maxlength of chars in the first field it would get enabled. And I want the cursor there on the next field automatically after getting enabled.

The code I am using is given below:

.directive('moveNext', function(){
return{
    restrict: 'A',
    link: function($scope, element){
        element.on("input", function(e){
            if((element.val().length==element.attr("maxlength"))){
                console.log('asdfasdf');
                var tabindex = 1;
                var $nextelement = $('input[tabindex='+(tabindex+1)+']');
                console.log($nextelement);
                if($nextelement){
                    console.log('asdf');
                    $nextelement.focus();
                }
            }
        });
    }
}
});
like image 281
Swapnil Rai Avatar asked Aug 10 '16 10:08

Swapnil Rai


People also ask

Does autofocus attribute always set focus on first input field in HTML5?

The first input or textarea in source order that has the autofocus attribute will be focused on page load. In browsers without autofocus support, no fields will be focused on page load, unless otherwise given focus with JavaScript.

What will happen when the disabled value is submitted?

A disabled input element is unusable and un-clickable. The disabled attribute can be set to keep a user from using the <input> element until some other condition has been met (like selecting a checkbox, etc.). Then, a JavaScript could remove the disabled value, and make the <input> element usable.

How do you autofocus input field in react JS?

Autofocusing using React useRef() and useEffect() Hooks We can also get the same functionality with the useRef() and useEffect() React Hooks. Here is the same form using these Hooks: import React, { useRef, useEffect } from "react"; const Form = () => { const emailInput = useRef(null); useEffect(() => { if (emailInput.


1 Answers

Try not using ng-disabled instead use disabled property of javascript.

document.getElementById("myText").disabled = true;

And then under your directive make above false first and move the focus on next element like as below:

document.getElementById("myText").disabled = false;
$nextelement.focus();

In your case focus is getting called first after digest cycle before the field gets enabled.

like image 53
Prashant Kumar Avatar answered Sep 28 '22 07:09

Prashant Kumar