javascript - Jquery Select Dynamic ID - Stack Overflow

I'm trying to toggle the visibility of this. You'll notice there's a primary key number

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?

Share Improve this question asked Feb 8, 2016 at 9:56 Amy NevilleAmy Neville 10.7k13 gold badges61 silver badges98 bronze badges 6
  • 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
 |  Show 1 more ment

1 Answer 1

Reset to default 4

Just 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745230915a4617673.html

相关推荐

  • javascript - Jquery Select Dynamic ID - Stack Overflow

    I'm trying to toggle the visibility of this. You'll notice there's a primary key number

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信