How to get input has focus or not using jquery [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
How to get input tag of html has focus or not using jquery
keydown event will work for form if input,image etc. tag has focus. but it will not work it focus is on form but not on any tags like input,image etc.
How can I solve this.
Please help
Thanks in advance
$('#title').focusout(function()
{
if($(document.activeElement).is(":focus"))
{
alert('focus');
}
else
{
alert('not focus');
}
});
// this will not work

you can do it by
if ($("#id").is(":focus")) {
alert('focus');
}

Its much easier to check by using jquery.
Using jquery u can do it like this
if($"#div/inputtocheck").is(":focus"))
{
//do something u want if it is in focus
}
The :focus returns true if the div is in foucs and false if it is not in focus

You can use document.activeElement.

Related

jquery on event remove all tags except span [duplicate]

This question already has answers here:
jQuery (almost) equivalent of PHP's strip_tags()
(9 answers)
Closed 8 years ago.
I'm looking for a function that's exactly like
$str = striptags($str, 'span');
in PHP. I need the same type of function in jquery:
$("#str").on('click keydown keyup change keypress', function(e) {
e.preventDefault();
???
)};
Is it simple like in PHP? or it's something more complicated?
Give this jQuery a try:
$('#str').on('click', function(e) {
$('body').find('*').not('span').each(function() {
$(this).remove();
});
});
You'll have to adjust its target to whatever parent you want to clear the content of, but this should work.
Edit
I've updated it to account for if the element is contained in a span, at some point:
$('#str').on('click', function(e) {
$('body').find('*').not('span').each(function() {
if(!$(this).parents().is('span')) {
$(this).remove();
}
});
});
If you only want to accommodate for a single level up, replace parents() with parent() in the if statement.
Here's a JSFiddle: http://jsfiddle.net/y1r0wg2r/

JQuery doubleclick listener in select - IE not working [duplicate]

This question already has answers here:
double click using IE
(2 answers)
Is there a workaround to trigger an option.click event in IE?
(1 answer)
Closed 8 years ago.
I seem to have a bit of trouble with Internet-Explorer once again...
The following code works fine in other browsers but IE doesn't like it at all!
It would be a simple double click listener for each option in a select.
Here's the simplified JSFiddle
http://jsfiddle.net/zoq8yo0s/1/
Any suggestions?
function registerClickListener() {
$(".available-option").each(function(){
$($(this)).dblclick(function(){
alert("dblclick");
});
});
}
Thanks! :)
Bind the dblclick event to the select element its working,
$(document).ready(function () {
$("#selectEl").dblclick(function () {
alert("dblclick");
});
});
Fiddle
You could start a timer on the click of an element and wait for a while for the second click. If second click comes, clear the timer and run double click. If the second click doesn't come, then it is a single click.
if .available-option is the id then use #available-option instead of .available-option
Hope will help u.
try this
$(document).on("dblclick",".available-option", function (){
//code here
});
or you can do this :
$(document).on("dblclick",".available-option", myfunction);
function myfunction(){
//code here
}

Javascript/jQuery detect if input is focused [duplicate]

This question already has answers here:
Using jQuery to test if an input has focus
(15 answers)
Closed 9 years ago.
How do I detect when .click event triggers if textarea is already focused?
I have this jquery:
$(".status").on("click","textarea",function(){
if ($(this) == "focused") {
// fire this step
}else{
$(this).focus();
// fire this step
}
With pure javascript:
this === document.activeElement // where 'this' is a dom object
or with jquery's :focus pseudo selector.
$(this).is(':focus');
If you can use JQuery, then using the JQuery :focus selector will do the needful
$(this).is(':focus');
Using jQuery's .is( ":focus" )
$(".status").on("click","textarea",function(){
if ($(this).is( ":focus" )) {
// fire this step
}else{
$(this).focus();
// fire this step
}
Did you try:
$(this).is(':focus');
Take a look at Using jQuery to test if an input has focus it features some more examples

Clear input textbox programmatically [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
jQuery Chosen reset
I'm using Chosen plugin on my project.
http://jsfiddle.net/tt13/gFzzc/
The problem is, I can't reset chosen input textbox programmaticaly. How to do it when I click clear button?
This works jsfiddle
$('button').click(function(){
$('.search-choice-close').click();
});
You might narrow down the scope a bit in case of multiple controls.
First of all, you should use the correct version of Chosen, taken from here:
https://raw.github.com/harvesthq/chosen/master/chosen/chosen.jquery.js
Then, simply reset the value of the select and trigger "liszt:updated" event:
$("button").on("click", function() {
$(".chzn-select").val("").trigger("liszt:updated");
});​
DEMO: http://jsfiddle.net/gFzzc/13/
You can try something like this :
Give Id to the button:
say clear_chosen
and then trigger close button:
javascript code::
$("#clear_chosen").click(function(){
$(".search-choice-close").click();
});
Write the code inside this block::
$(function () {
$('.chzn-select').chosen();
});
Ugly hack, but works:
Demo: jsfiddle
Code:
$('button').click(function() {
$(".chzn-select").next('.chzn-container').find('.search-choice').remove();
$(".chzn-select").val('');
$(".chzn-select").next('.chzn-container').find('.search-field input').focus();
});

How can I remove an attribute with jQuery?

I can't seem to get removeAttr to work, I'm using the example I saw on the jQuery site. Basically onclick I add the attribute to disable a field (which works just fine) but when the user clicks again it should enable the field in question. I used alerts to make sure the else block is being fired, so I know that's not it.
Code:
$('#WindowOpen').click(function (event) {
event.preventDefault();
$('#forgot_pw').slideToggle(600);
if('#forgot_pw') {
$('#login_uname, #login_pass').attr('disabled','disabled');
} else {
$('#login_uname, #login_pass').removeAttr('disabled');
}
});
Thanks.
All good used this:
$('#WindowOpen').toggle(
function()
{
$('#login_uname, #login_pass').attr("disabled","disabled");
},
function()
{
$('#login_uname, #login_pass').removeAttr("disabled");
});
Your problem is that the following line of code will always evaluate to true.
if('#forgot_pw')
try replacing with
if($('#forgot_pw').attr('disabled'))
$('#forgot_pw').attr('disabled', false);
should work for you.

Categories

Resources