hc2p
hc2p

Reputation: 188

chrome extension popup textarea focus

It seems that i cannot call focus() on a textarea of my chrome extensions popup when it gets opened / after ondomready.

i have something like that in popup.js :

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

i reference it in popup.html like that:

<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>

The css-change works, the focus() does not!

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

Any help highly appreciated!

Upvotes: 4

Views: 2498

Answers (5)

David Ringsmuth
David Ringsmuth

Reputation: 335

I use this to focus.

My element has "autofocus" as an attribute. When the form is loaded, the element with the attribute "autofocus" gets the focus.

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

Upvotes: 0

IanW
IanW

Reputation: 773

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
}

Works for me!

Upvotes: 3

hc2p
hc2p

Reputation: 188

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

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

Upvotes: 4

abraham
abraham

Reputation: 47833

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

Upvotes: -1

hamczu
hamczu

Reputation: 1774

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

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

Upvotes: 0

Related Questions