input element focus() not working [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
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

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!

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();

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

Related

How to invoke jQuery function on document ready? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Trying to do something once HTML is loaded and document get ready. However, code doesn't seems to work.
HTML:
<div class="dataprocess"></div>
JS:
$(document).ready(function(){
if( $('div').hasClass('dataprocess') ) {
console.log('working');
}
});
Not able to get working on console. What am I doing wrong?
In your snippet, you're missing the closing brackets for the $(document).ready() function.
$(document).ready(function(){
if($('div').hasClass('dataprocess')){
console.log('working');
}
});
This is the only visible issue in your code.
Also some other things you may have forgotten:
Include the JavaScript file
Include the jQuery dependency
I think the best option is to use id to call the element:
<div id="data_id" class="dataprocess"></div>
and then JS
$(document).ready(function(){
if($( "#data_id" ).hasClass( "dataprocess" )){
console.log("working");
}
});

Can't remove a list item? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am trying to remove a list item.
I add an item in like so:
$('#btnAdd').click(function(){
var text = $('#item').val();
if(text.length){
$('<li />',{html: text}).appendTo('ul.justList')
}
});
then remove it like so:
$('#btnRemove').click(function(){
$("#justList li:last-child").remove();
});
Why does the last list item not remove? Any suggestions? Thanks so much!
I think the problem may be in your punctuation. Note the difference in punctuation between "justList" in .appendTo('ul.justList') and in $("#justList li:last-child").
ul.justList says, "Look for a <ul> with the class of 'justList'," or <ul class="justList">.
#justList says, "Look for any element with the ID of 'justList'," or <any-element id="justList">.
Classes and IDs are different, so make sure you're using the correct punctuation: . for a class, and # for an ID.
Based on your add statement I would guess that you need a remove statement like:
$('#btnRemove').click(function(){
$("ul.justList li:last-child").remove();
});
If your add statement is working then this should be accessing the same ul
Please use . instead of # while remove as well
$('#btnRemove').click(function(){
$(".justList li:last-child").remove();
});

How to get the id of an item in an array? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
var a = document.getElementById('abc');
alert(a.id) // it works.
var b = document.getElementsByClassName('qwe');
alert(b[0].id); // this is not working. returns only empty space : ''
I don't know why this happening.
a is object, and b is array object. and b[0] is object property. and also object.
Is there any one who can explain this happening's reason and solution clearly?
ps. sorry for miss 'document'. I missed it when I write this question. but in the source code, I didn't miss it.
<html>
<div id="abc" class="qwe"></div>
<div id="a1" class="qwe"></div>
<div id="a2" class="qwe"></div>
<div id="a3" class="qwe"></div>
if you write document before getElementById it should work fine .
Here is jsBin example JSBIN
var a = document.getElementById('abc');
alert(a.id) // it works.
var b = document.getElementsByClassName('qwe');
alert(b[0].id); // this is not working. returns only empty space : ''
JSFiddle
You forget to use document before getElementById and getElementsByClassName, other wise its working fine. check out above jsfidlle link

What is causing my jQuery functions not run? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I can't figure out why the jQuery functions will not work with the html here
JS Fiddle: http://jsfiddle.net/JonaTheApprentice/E89rg/
function addItem() {
$('#list').append('<li><input type="checkbox" /> item</li>');
}
function removeItem() {
$('#list').children().filter(function () {
return this.firstChild.checked;
}).remove();
}
I don't see an element with id="list", that's what #list refers to.
I think you mean #items-listed. Changing that, and it works
I would get the value from the input id. I would also assign a click event for cleaner coding. Here is an example of your fiddle adding an item. Removing an item is just the reversal.
DEMO http://jsfiddle.net/E89rg/5/
$('#Enter').click(function(){
var Val = $('#item').val();
$('#items-listed').append('<li><input type="checkbox" />'+Val+'</li>');
});

$("divId").text();....is not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I have a contenteditable div inside jquery tab and I am trying to get the content using a
button. When I click on button a javaScript function is getting called,
inside the function I am trying to grt the content
HTML code
<div id="tab1" contenteditable="true">
<div>line no one</div>
<div>line no one</div>
</div>
JavaScript function
alert($("tab1").text());
alert($("tab1").html());
error
For text(); I am not getting any thing but for html(); it is showing Undefined
I am not able to guess whats wrong with the code...
Any suggestions...
You need to use ID indication "#"
alert($("#tab1").text());
// ^here
DEMO
See you have missed # notation for id selector and make sure to have it wrapped in $(function(){...}) doc ready handler:
$(function(){
alert($("#tab1").text());
alert($("#tab1").html());
});
jQuery expects a selector, not an id. "tab1" is a type selector, it matches <tab1> elements (which do not exist in HTML).
You can use an ID selector though: $("#tab1").
Are you using jquery?
Does it load proprely?
try this
alert($("#tab1").text());
alert($("#tab1").html());
you have to tell jquery that you are looking for an id with "#"

Categories

Resources