admin管理员组文章数量:1333417
After pasting text, I want to get the ID of the target div. Later the divs are generated dynamically, code does not work:
js fiddle
HTML
<div class="wrap">
<div class="entry" id="1" contenteditable="true">paste here</div>
<div class="entry" id="2" contenteditable="true">paste here</div>
<div class="entry" id="3" contenteditable="true">paste here</div>
<div class="entry" id="4" contenteditable="true">paste here</div>
</div>
JS
$('.wrap').on("paste", function(){
console.log("current box ID: " + $(this).attr("id") );
});
After pasting text, I want to get the ID of the target div. Later the divs are generated dynamically, code does not work:
js fiddle
HTML
<div class="wrap">
<div class="entry" id="1" contenteditable="true">paste here</div>
<div class="entry" id="2" contenteditable="true">paste here</div>
<div class="entry" id="3" contenteditable="true">paste here</div>
<div class="entry" id="4" contenteditable="true">paste here</div>
</div>
JS
$('.wrap').on("paste", function(){
console.log("current box ID: " + $(this).attr("id") );
});
Share
Improve this question
asked Apr 14, 2014 at 17:29
user2952265user2952265
1,6309 gold badges22 silver badges35 bronze badges
6 Answers
Reset to default 6Try you're using parent class.wrap
You need to use .entry
class as you are pasting in that div
$('.wrap div.entry').on("paste", function(){
console.log("current box ID: " + $(this).attr("id") );
});
If .entry
is created dynamically then you can use event delegation
$(document.body).on('paste','.wrap .entry',function(){
console.log("current box ID: " + $(this).attr("id") );
});
Fiddle
You need to look at the target of the event (which is where the event originated) and then find the .entry
which wraps that. You can't necessarily look directly at the target if you want to support having any plex structure inside of the .entry
elements.
$('.wrap').on('paste', function(event){
console.log('paste id: ' + $(event.target).closest('.entry').attr('id'));
});
Not sure why you're attaching the event against the "container". I believe you have 2 options
$('.wrap').on("paste", function(e){
console.log("current box ID: " + e.target.id );
});
or
$('.entry').on("paste", function(e){
console.log("current box ID: " + this.id );
});
I'd go with the second option since it's more more clear to attach the event direclty to relevant elements instead of doing it to the container and use the e.target
approach.
You are binding the event to the wrap div, so that's what this
means for in the callback.
Change your event binding to:
$('.wrap .entry').on("paste", function(){
console.log("current box ID: " + $(this).attr("id") );
});
I'd go with this (just a little less code, but not much -
$('.entry').on("paste", function(e){
console.log("current box ID: " + this.id ); // this.id is all you need to get any current id
});
Because you binded the 'paste' event on the wrap class (which doesn't got an attribute id) you get a NULL.
You have to bind it you your entry fields, which have the id attribute.
$('.wrap .entry').on("paste", function(){
console.log("current box ID: " + $(this).attr("id") );
});
本文标签: javascriptjQueryget ID of element after paste eventStack Overflow
版权声明:本文标题:javascript - jQuery - get ID of element after paste event - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742336662a2455763.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论