user3487944
user3487944

Reputation: 277

focus not working for input

MY focus is not working

Code on JSFiddle:

http://jsfiddle.net/JLULx/

HTML

<body>
    Email1<input type="email"><br/>

    <script src="jquery-1.7.1.min.js" type="text/javascript"></script>
    <script src="selectors.js" type="text/javascript"></script>
</body>

Script ...

$(document).ready(function () {

    var email_default = "Enter your Email address";
    $('input [type="email"]').focus();
});

similar post :

  1. $.focus() not working

it says $(document).ready function should load completely and focus should be shift from console ..how to do it . I could't understand ...

2.jQuery focus function not working in Firefox

This is doesnt seem to be relevant ..

Please suggest

Upvotes: 2

Views: 13967

Answers (4)

Vijay
Vijay

Reputation: 3023

There are two things missing on you fiddle - 1. You haven't included proper jQuery library instead trying with pure JS. 2. in your selector remove the space between 'input' and '[type="email"]'. (as answered by RPM)

Upvotes: 0

Ryan
Ryan

Reputation: 14649

To check if jQuery returned an object do this:

if($('.my-selector').length)) {
  // jQuery returned an object
}

You have an extra space in your selector. jQuery thinks your trying to access a child element of an input element. You need to change your code to this:

$('input[type="email"]').focus();

Also be sure that you are loading the jQuery library before you load your custom JavaScript.

http://jsfiddle.net/JLULx/2/

Upvotes: 0

Eduardo
Eduardo

Reputation: 258

Mmmmm, I think the problem is with JSFiddle.

For example if you run $("#run") on the browser console. It returns "null". It's supposed the ID "run" belongs to the run button on JSFiddle

Upvotes: 0

Eduardo
Eduardo

Reputation: 258

Why don't you use an ID for the email field.

Example:

<input id="input-email" type="email"/>


$("#input-email").focus()

A more simple way is to use the HTML5 autofocus feature:

<input type="email" autofocus="true">

Upvotes: 2

Related Questions