admin管理员组文章数量:1415476
I'm trying to toggle the visibility of this. You'll notice there's a primary key number at the end of the id:
<div id="campaign-details-container-12">
And this is how I've tried to select it:
$(document).on("click",".show-details",function () {
$(this).blur();
var id = $(this).data("id");
var selector = "#campaign-details-container-"+id;
alert(selector);
$(selector).toggle();
return false;
});
How can I get this to work? I could put a data-id="12"
tag in the div instead and use a class, but how would I select by that?
I'm trying to toggle the visibility of this. You'll notice there's a primary key number at the end of the id:
<div id="campaign-details-container-12">
And this is how I've tried to select it:
$(document).on("click",".show-details",function () {
$(this).blur();
var id = $(this).data("id");
var selector = "#campaign-details-container-"+id;
alert(selector);
$(selector).toggle();
return false;
});
How can I get this to work? I could put a data-id="12"
tag in the div instead and use a class, but how would I select by that?
- show-details is a separate button – Amy Neville Commented Feb 8, 2016 at 10:01
-
1
In the fiddle of @ArunPJohny,
show-details
is separated. What do you need? – Marcos Pérez Gude Commented Feb 8, 2016 at 10:02 - 2 Your initial version should work as-is. What does the HTML look like? – iCollect.it Ltd Commented Feb 8, 2016 at 10:03
- 1 jsfiddle/arunpjohny/aeo6oo70/3 – Arun P Johny Commented Feb 8, 2016 at 10:03
- 1 @AmyNeville Maybe api.jquery./category/selectors can help. – Reporter Commented Feb 8, 2016 at 10:04
1 Answer
Reset to default 4Just select it using the attribute starts with syntax:
$("[id^=campaign-details-container]");
No need to go do unnecessary things with classes and id variables...
See http://www.w3schools./cssref/sel_attr_begin.asp
本文标签: javascriptJquery Select Dynamic IDStack Overflow
版权声明:本文标题:javascript - Jquery Select Dynamic ID - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745230915a2648819.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论