JQuery clear divs when click off input but not off page - javascript

I am trying clear a div when a user clicks or tabs off of an input, but I don't want the div cleared if the user changes to a different window or browser tab.
What I have so far clears the div in both cases:
$input.on('blur focus mousedown', function () {
$input.val("");
$("#cors-test-invalid").html("");
$("#cors-test-views").html("");
$("#cors-test-workbooks").html("");
$("#cors-test-datasources").html("");
$("#cors-test-projects").html("");
$("#cors-test-users").html("");
});

You can do it like this
$(document.body).on('click',function(){
//here its a click on body which eliminates the other tabs or windows
if(!$input.is(":focus"))
{
//here element doesnt have focus, so we are good to do the logic
$input.val("");
$("#cors-test-invalid").html("");
$("#cors-test-views").html("");
$("#cors-test-workbooks").html("");
$("#cors-test-datasources").html("");
$("#cors-test-projects").html("");
$("#cors-test-users").html("");
}
});

Related

hide toggle content, if a user didnt click inside a specific box

I have this code for toggling menus on my site. It will open a menu on click, close others open when you click another and close them all if you click outside.
The issue is, I'm now using this for my search bar to appear too, but if you click inside the search box it vanishes - woops. Would it be possible to amend the hiding code to detect if the user wasn't clicking inside a specific area of the code?
// navbar toggle menu
$(document).on('click', ".toggle-nav > a", function(event) {
event.preventDefault();
event.stopPropagation();
var $toggle = $(this).closest('.toggle-nav').children('.toggle-content');
if ($toggle.hasClass('toggle-active'))
{
$($toggle).removeClass('toggle-active');
}
else
{
$(".toggle-content").removeClass('toggle-active');
$($toggle).addClass('toggle-active');
}
});
// hide the toggle-nav if you click outside of it
$(document).on("click", function ()
{
$(".toggle-content").removeClass('toggle-active');
});
Instead of using click, this uses mouseup. If the target is, for example #search-bar, it won't remove toggle-active from toggle-content elements.
$(document).mouseup(function(e) {
if (!$(e.target).is('#search-bar')) {
$(".toggle-content").removeClass('toggle-active');
}
});
You can see it in action with this jsFiddle.
Hopefully this helps.

Form in popup, disabling child click to close blocking checkbox

I have a popup window that has a form in it and to stop the popped up div clicks from closing the window I've used:
$(".popup").click(function () {
$(".popup").css("display", "none");
}).children().click(function(e) {
return false;
});
However this works perfectly except with checkboxes whereby it disables checking them.
Is there any way around this?
popup is an overlay, then popupInner holds the form.
Try replacing your code with this:
$(".popup").click(function(e) {
if (e.target == this) $(this).hide();
});
It would hide the popup only if clicked on the element with class popup and not its children, it would also keep the functionality of the children intact.

how can I check if a particular item is clicked with .blur() in jquery

I have an autocomplete dropdown that appears when a user starts typing in a textbox (I'm using jquery mobile but I don't think that's important to my problem). I want to be able to hide the whole dropdown list when a user clicks anywhere on the page. However, I don't want to hide the dropdown when a user actually clicks on the dropdown itself.
Is there a way I could catch the click event in order to know what was clicked?
Here's my blur function:
//hide autocomplete when dropdown is not clicked
$("#search-div input").blur(function () {
$("#autocomplete-list").hide();
});
I was thinking of somehow putting an if statement in my blur function. Here's my pseudo code:
if( dropdown clicked)
{
run code to take text from dropdown and place in textbox
}
else
{
hide dropdown
}
Would it be possible to know whether my dropdown is clicked or something else is clicked while in my blur function? When I debug my javascript I'm only seeing an event that's related to the textbox doing the blur()
Edit:
Here is a function I'm using to handle when the dropdown is clicked:
$( document).on( "click", "#autocomplete-list li", function() {
var selectedItem = event.target.innerHTML;
$(this).parent().parent().find('input').val(selectedItem);
$('#autocomplete-list').hide();
runSearchQuery();
});
You can listen for any click, not just a blur, and then check what the clicked element was. e.currentTarget gives you what was clicked.
var clickHandler = function(e) {
if ($(e.currentTarget).hasClass('dropdown')) {
// do nothing
} else {
// Make sure you unregister your event every
// time the dropdown is hidden.
$(window).off('click', clickHandler);
// hide
}
}
// When the dropdown comes down, register an event on the whole page.
$(window).on('click', clickHandler);

jQuery popup / hide box

Here is where I am at so far:
http://jsbin.com/ujuqa3/4
So far, I've decided to set a variable to false and to true when the .share-box is open. After it is open, I want the user to be able to click anywhere (except the box) to close it.
Right now it works the first time, but any time after that, it messes up for some reason.
$(document).ready(function() {
// user clicks on report this button
$(".shareThis").click(function() {
// confirmation fades in
$(".share-box").fadeIn("fast"),
// Prevent events from getting pass .share-box
$(".share-box").click(function(e){
e.stopPropagation();
});
});
$(document.body).click(function () {
$("body").click(function(){
// hide the share-box if clicked anywhere aside from the box itself
$(".share-box").fadeOut().removeClass("active");
});
});
});
Add the return false;
// user clicks on report this button
$(".shareThis").click(function() {
// confirmation fades in
$(".share-box").fadeIn(),
// Prevent events from getting pass .share-box
$(".share-box").click(function(e){
e.stopPropagation();
});
return false;
});
When $('.shareThis') click happen its also triggering the $(document.body).click
build a demo at
http://jsbin.com/uyizi4/4

JQuery blur event

I have a text area and a "add" button on a form. By default the text area is shown as shrinked and grayed out. On focus it will be highlighted by changing the style. On blur it should go back to previous state. Along with highlighting the textarea, add note should toggle as visible and hidden. The problem is when i enter the data in text area and click on the add button, the blur event is hiding the add button and the event on the add button is never triggered.. any solution??
It seems like my question is not clear...
The solution is something like execute the blur event except that the next focused element is not the "add" button...
If there is text entered is it safe to assume you don't want to toggle the button because the user has the intention of entering a note? If so you could do something like:
$(textBox).blur(function() {
if($(this).val().length == 0) {
//change the style
//hide the button
}
})
You need to unhighlight your textarea and hide the "add" button only if there is nothing entered in the textarea:
$('textarea').blur(function() {
if($(this).val() != '') {
// unhighlight textarea
// hide "add" button
}
});
This way the user always sees the field and button if they actually entered something into it, regardless of whether it has focus or not.
Put a small delay between the focus leaving the textbox and the button being hidden, e.g.
function textBox_blur(evt)
{
window.setTimeout(
function() { button.style.display = 'none'; },
200 // length of delay in milliseconds
);
}
This will leave enough time for the click to process (though you might want to play with the length of the delay)

Categories

Resources