Javascript/jQuery detect if input is focused [duplicate] - javascript

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

Related

RemoveClass not working on a class name just added using jquery [duplicate]

This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 6 years ago.
For some reason when I add a class to an element using Jquery I cannot perform a function using the class name just added:
$('.openslidecontent .arrow.off').click(function() {
$('.openslidecontent,.rightwrapper .arrow').removeClass('off')
$('.openslidecontent,.rightwrapper .arrow').addClass('on');
return false;
});
$('.openslidecontent .arrow.on').click(function() { // THIS FUNCTION DOES NOT EXECUTE
$('.openslidecontent,.rightwrapper .arrow').removeClass('on');
$('.openslidecontent,.rightwrapper .arrow').addClass('off');
return false;
});
The event listener is only attached to existing elements. You can switch to event delegation to have the listener react on newly created elements too
$(document).on('click', '.openslidecontent .arrow', function() {
$('.openslidecontent,.rightwrapper .arrow').toggleClass('on off');
})
Also, as Rory commented, you can just change your code to use toggleClass() and hook the event to both at the same time

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/

Alert when dynamic button is clicked [duplicate]

This question already has answers here:
jquery - Click event not working for dynamically created button [duplicate]
(4 answers)
Jquery/Javascript: Buttons added dynamically with javascript do not work
(3 answers)
Closed 8 years ago.
I can't get an alert to trigger when a dynamically generated button is clicked?
The alert is not going to be the final function but was a test to make sure the trigger is working correctly.
I tried a "onclick" function as a trigger and used the id as a jQuery trigger so not sure why it would not work; I will add a snippet below that shows what I mean.
From the file that generates and displays the button (it displays OK):
var modOptsMsg = document.getElementById("modOptionsMessage").value + '<input type="button" id="removePost" onclick="removePost()" value="Remove Post"/>';
$("#modOptsShowMsg").empty().append(modOptsMsg);
Neither of these simple tests work in JS or jQuery:
function removePost(){
alert("alert");
}
$('#removePost').click(function(){
alert("alert");
});
As #Regent has pointed out in the comments, use:
$(document).on("click", '#removePost', function() {
alert("alert");
});
$('#modOptsShowMsg').on("click", '#removePost', function() {
alert("alert");
});
the above code will work..if jquery is lesser than 1.7 use .live() instead of .on()

How to distinguish between mouse click and .click() in javascript/jquery? [duplicate]

This question already has answers here:
In jQuery, how can I tell between a programmatic and user click?
(7 answers)
Closed 8 years ago.
In javascript/jquery, I have a button that have a click event defined like:
$("#target").click(function() {
var mouse_click = ________________;
// do stuff
if (mouse_click) {
// do stuff
}
// do stuff
});
and I also have code to do
$('#target').click();
How can I get the variable mouse_click to be true, if the user manually clicked the tag using the mouse, and the variable to be false when I click the tag using the .click() function?
Thanks
You could use this:
$("#target").click(function(e) {
var mouse_click = !(e.originalEvent === undefined);
// do stuff
if (mouse_click) {
// do stuff
}
// do stuff
});
or you could check e.isTrigger which is set whenever an event is triggered within jQuery. You'd just need to change the second line to:
var mouse_click = !e.isTrigger;
Both will work but you might prefer the second option as it's a little more concise.
Here it is working: http://jsfiddle.net/2U3Us/4/

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

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.

Categories

Resources