admin管理员组文章数量:1356869
There is an element to which is dynamically applied data attribute like this
var tr = $('<tr>');
tr.data('template_id', 5);
table.find('tbody').append(tr);
Here we have our tr on the table.
...
Here we want to find our newly added tr by data attribute
var tr = table.find('[data-template_id="5"]');
console.log(tr) // tr.length = 0;
Unfortunately, we have no found tr this way.
Why it is getting this result?
There is an element to which is dynamically applied data attribute like this
var tr = $('<tr>');
tr.data('template_id', 5);
table.find('tbody').append(tr);
Here we have our tr on the table.
...
Here we want to find our newly added tr by data attribute
var tr = table.find('[data-template_id="5"]');
console.log(tr) // tr.length = 0;
Unfortunately, we have no found tr this way.
Why it is getting this result?
-
What is
table
in your first code snip? I think you need$('table')
– Patel Commented Jul 14, 2015 at 8:57 - Yes. var table = $('table'); – abdulmanov.ilmir Commented Jul 14, 2015 at 8:59
3 Answers
Reset to default 11The issue you have is that data-*
attributes added via jQuery are stored in its internal cache. They are not available as attributes in the DOM. With that in mind, you can use filter()
to achieve what you require:
var $tr = table.find('tr').filter(function() {
return $(this).data('template_id') == 5;
});
console.log($tr.length);
Try tr.attr("data-template_id", 5)
instead of tr.data('template_id', 5)
.
I think you can use
tr.attr('template_id', 5);
本文标签: javascriptHow to find dynamically added element by data attribute using jQueryStack Overflow
版权声明:本文标题:javascript - How to find dynamically added element by data attribute using jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744035762a2579740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论