Reputation:
Html
<input type="text" id="match1">
<div id="divf">It is a div which has a dark future</div>
<button>Not Remove</button>
and script
$(function(){
$("input").blur(function()
{
$("#divf").remove();
});
});
Now as it is clear from code i want the div to be removed on blur on input .But i don't want div to be removed if someone is clicking on button .I have no idea how to do it.Can somebody solve this problem
Upvotes: 1
Views: 217
Reputation:
I have editted @Adil answer and got what i wanted
$(function(){
var isButtonClicked = false;
$('button').one('mousedown', function(){
isButtonClicked = true;
});
$("input[type=text]").blur(function()
{
if(!isButtonClicked)
$("#divf").remove();
else isButtonClicked = false;
});
});
Upvotes: 0
Reputation: 148110
If you want to put on just text field having id match1 then div will be removed only when the field has blue
$(function(){
var isButtonClicked = false;
$('button').one('click', function(){
isButtonClicked = true;
});
$("#match1").blur(function()
{
if(!isButtonClicked)
$("#divf").remove();
});
});
If you want to put on just type text then the div will be removed on blue of every text box
$(function(){
var isButtonClicked = false;
$('button').one('click', function(){
isButtonClicked = true;
});
$("input[type=text]").blur(function()
{
if(!isButtonClicked)
$("#divf").remove();
});
});
Upvotes: 0
Reputation: 173552
You could set a timeout in the .blur()
and then clear it when the button is clicked.
var to = false;
$('input').blur(function() {
// start the timer
to = setTimeout('removeDiv', 200);
});
$('button').click(function() {
// if the timer is still running, clear it
to && clearTimeout(to);
});
function removeDiv()
{
$("#divf").remove();
// reset timer
to = false;
}
When the focus goes from the input straight to the button (it's clicked), the timer is cleared and the div is not removed.
Upvotes: 5
Reputation: 429
Use this code instead of your code
$(function(){
$("#match1").blur(function()
{
$("#divf").remove();
});
});
Upvotes: 0