admin管理员组文章数量:1277257
I'm dynamically adding an element in jQuery, and upon doing so, I'm focusing on the element. I'm trying to then remove this element on blur
. However, the blur
event or the focusout
event don't seem to be firing when the focus is lost.
Appending the element:
var searchForm = '<input id="header-search" type="text" class="form-control" placeholder="Search">';
$('body').append(searchForm);
$('#header-search').focus();
Binding to the blur()
event:
$('#header-search').on('blur', function() {
alert('blur');
$(this).remove();
});
EDIT: I just created a jsFiddle, and it seems to function as I want. Strange. /
I'm dynamically adding an element in jQuery, and upon doing so, I'm focusing on the element. I'm trying to then remove this element on blur
. However, the blur
event or the focusout
event don't seem to be firing when the focus is lost.
Appending the element:
var searchForm = '<input id="header-search" type="text" class="form-control" placeholder="Search">';
$('body').append(searchForm);
$('#header-search').focus();
Binding to the blur()
event:
$('#header-search').on('blur', function() {
alert('blur');
$(this).remove();
});
EDIT: I just created a jsFiddle, and it seems to function as I want. Strange. https://jsfiddle/danhaswings/cpczLL7g/
Share Improve this question edited Feb 10, 2016 at 12:12 Shashank Agrawal 25.8k11 gold badges96 silver badges125 bronze badges asked Feb 10, 2016 at 12:03 Daniel DewhurstDaniel Dewhurst 2,6032 gold badges24 silver badges40 bronze badges 1- 1 use: event delegation method – Bhojendra Rauniyar Commented Feb 10, 2016 at 12:03
5 Answers
Reset to default 7Just change it to:
$(document).on('blur', '#header-search', function() {
alert('blur');
$(this).remove();
});
Since your element is being added dynamically and when your code is executed, no DOM element is found with selector #header-search
. So, you need to change the code like above to fix for dynamically added elements.
If your element is appended before adding your event, you need to bind the event to a parent that already exists using $(document).on( eventName, selector, function(){} );
.
This is Event delegation and you have to use .on() using delegated-events approach
$(document).on('blur', '#header-search', function() {
alert('blur');
if ($(this).val() == '' && $searchButton.is(':active') == false) {
$(this).remove();
}
});
var searchForm = '<input id="header-search" type="text" class="form-control" placeholder="Search STRI...">';
$('body').append(searchForm);
$('#header-search').focus();
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
When trying to bind an event to any 'Dynamically added element', it'd be better to write
$(document.body).on("blur", "#header-search", function () {
// do anything you wanna do...
alert("blur");
$(this).remove();
});
The problem was the $searchButton.is(':active')
on line 7, the var was not defined.
in this fork is working fine https://jsfiddle/w5jpypcy/
In your code you must have added Jquery first and then the HTML like
$('#header-search').on('blur', function() {
alert('blur');
if ($(this).val() == '' && $searchButton.is(':active') == false) {
$(this).remove();
}
});
var searchForm = '<input id="header-search" type="text" class="form-control" placeholder="Search STRI...">';
$('body').append(searchForm);
$('#header-search').focus();
that is why it is working in your JSFiddle where you have added Blur event after you created your runtime input tag and not in your environment.
Hope this helps.
本文标签: javascriptjQuery on blurfocusout not firing on dynamically added elementStack Overflow
版权声明:本文标题:javascript - jQuery on blurfocusout not firing on dynamically added element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741201367a2357289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论