Chrome extension popup textarea focus

It seems that I cannot call focus () in the text box of my chrome popup when it opens / after ondomready.

I have something like this in popup.js:

$(document).ready(function () { console.log($("#moped-text")); $('textarea').focus(); $('.container').css("color", "red"); }); 

I refer to it in popup.html as follows:

 <html> <head> <link rel="stylesheet" href="css/content.css" type="text/css"> <script type="text/javascript" src="js/lib/jquery.js"></script> <script type="text/javascript" src="js/popup.js"></script> </head> <body> <div class="container"> <textarea name="newText"></textarea> </div> </body> 

Work css-shift, focus () does not work!

When I debug the popup and type $ ('textarea'). focus (); it works in the console. Also, events added inside the ready callback are successfully connected.

Any help is much appreciated!

+4
source share
5 answers

I still don't know why the focus is not set in the first place, but a simple timeout does the job :)

 setTimeout(function() { $('#moped-text').focus(); }, 500); 
+3
source

Workaround for Chrome 18 (found here )

 if(location.search !== "?foo") { location.search = "?foo"; throw new Error; // load everything on the next page; // stop execution on this page } 

It works for me!

+3
source

You have a div with class "container" not id. So use this instead:

 $('.container').css("color", "red"); 
0
source

I use this to focus.

My item has autofocus as an attribute. When a form loads, an element with the "autofocus" attribute gains focus.

.directive ('autofocus', ['$ timeout', function ($ timeout) {return {restrict: "A", link: function ($ scope, $ element) {$ timeout (function () {$ Element [0] .focus ();});}}}])

0
source

You can simply use <textarea name="newText" autofocus></textarea> .

-1
source

Source: https://habr.com/ru/post/1400726/


All Articles