0

My focus on the input doesn't work

HTML

$('input[name=persons]').focus();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html ng-app"myApp" ng-controller="person.ng.Controller as myCtrl" class="ng-scope">
    <input type="text" class="form-control ng-empty ng-valid ng-invalid-required ng-touched" name"persons" ng-model="persons.name" nq-required="isValid('persons.name')" required="required">

I do not know what I'm doing wrong. Please suggest

AT82
  • 71,416
  • 24
  • 140
  • 167
Mican luk
  • 11
  • 1
  • 1
  • There's not enough information here. I see that your selector is wrong first off. should be [`name="persons"`] but if that's not it, It could be any number of things. Maybe JQuery's not included, you're not putting the code in the appropriate function, etc. Please give us more information. – zfrisch Jun 27 '18 at 16:27
  • Wrap persons in quotes – abhikr7 Jun 27 '18 at 16:27
  • 2
    `name"persons"` should be `name="persons"`. Try this out. Hope it works. – Vikas Jun 27 '18 at 16:28
  • 2
    @zfrisch - The quotes are entirely optional when the value fits the definition of a CSS identifier. – T.J. Crowder Jun 27 '18 at 16:29
  • 1
    Clarifying @Vikas's comment: On the `input` element, you have `name"persons"` rather than `name="persons"`. Consequently, the element has no `name`. Voting to close as typo/non-repro/not-useful-to-others-in-future. – T.J. Crowder Jun 27 '18 at 16:31
  • Possible duplicate of [$.focus() not working](https://stackoverflow.com/questions/15859113/focus-not-working) – GSazheniuk Jun 27 '18 at 16:31
  • @T.J.Crowder I stand corrected. That seems *really* odd syntactically, but if it works it works. – zfrisch Jun 27 '18 at 16:32
  • @zfrisch - :-) [It's specified behavior](https://www.w3.org/TR/selectors-3/#attribute-representation), and indeed does work. It's kind of like attribute values in HTML. You don't need quotes around them, either, if the value doesn't contain any of ``"'<> `=`` ([link](https://html.spec.whatwg.org/#attributes-2)). – T.J. Crowder Jun 27 '18 at 16:55

3 Answers3

1

You have two issues to deal with:

First, you missed the "=" when setting the name to your input:

  • Your code: name"persons"
  • Correct code: name="persons"

Second, if your script is being created/referenced before the html elements, it means that you are trying to use your input before it is even created.

When using JQuery to access HTML fields, you should use:

$( document ).ready(function() {
    $('input[name=persons]').focus();
});

It's a inner function that will be executed only after your whole HTML page is loaded.

Ps. make sure you are importing JQuery correctly: Importing JQuery

Try it out!

RKrum
  • 410
  • 5
  • 15
  • His `ng-app"myApp"` also needs to be `ng-app="myApp"`. Its missing the equals sign too, but your answer already solves his question. – BStill Jun 27 '18 at 16:40
0

Set focus on the first text field:

 $(".form-control").focus();

 $("input:text:visible:first").focus();

This also does the first text field, but you can change the [0] to another index:

$('input[@type="text"]')[0].focus(); 

Or, you can use the ID:

$("#someTextBox").focus();
jvk
  • 2,133
  • 3
  • 19
  • 28
0

I used this Script and focused the last element input:

$(".form-control").focus();

I need focus only input="persons"

var x=$(".form-control");
for(var i=0; i<x.length; i++){
 if(x[i].getAttribute("name")==="persons"){
 console.log(x[i].getAttribute("name"));
 x[i].focus();
}
}

up script not working

Mican luk
  • 11
  • 1
  • 1