javascript - Jquery Select Dynamic ID -


i'm trying toggle visibility of this. you'll notice there's primary key number @ end of id:

<div id="campaign-details-container-12"> 

and how i've tried 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 work? put data-id="12" tag in div instead , use class, how select that?

just select using attribute starts syntax:

$("[id^=campaign-details-container]"); 

no need go unnecessary things classes , id variables...

see http://www.w3schools.com/cssref/sel_attr_begin.asp


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -