anton2g anton2g - 4 months ago 11
Javascript Question

JQuery clear divs when click off input but not off page

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

Answer

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

}

});