javascript - Get numerical value from parent with id like 'post-1' and use it in jQuery function -
i'm trying figure out following.
i have following jquery code:
var = ""; var bplay = 0; audiojs.events.ready(function() {     = audiojs.createall();     $(".audiojs .play-pause").click(function() {         var e = $(this).parents(".audiojs").index(".audiojs");         $.each(as, function(t, n) {             if (t != e && as[t].playing) {                 as[t].pause()             }         })         bplay = !bplay;         if (bplay == 1) {             $(".bar").each(function(i) {                 fluctuate($(this));             });         } else {             $(".bar").stop();         }     }) }); in nutshell preforms list of things when clicks particular .audiojs instance on page. 1) checks if there other instance playing, if there pauses it. , if playing applies fluctuate function elements on page have class="bar". issue! don't want apply .bar's on page, specific group associated particular .audiojs instance (the 1 being clicked , playing). 
i thought of following solution. each .audiojs instance inside div tag has id "post-1", "post-2" etc.. numerical value post id database. can add numerical id bar, bar-1, bar-2 etc... after i'm having issues.
for javascript work need retrieve numerical value "post-[id]" associated audiojs instance being clicked , store somehow, can use afterwards
bplay = !bplay;     if (bplay == 1) {         $(".bar-[value retrieved post-...]").each(function(i) {             fluctuate($(this));         });     } else {         $(".bar-[value retrieved post...]").stop();     } could explain me how can achieved?
honestly, easiest way stick in custom data-* attribute on <div id="post-x"> element, so:
<div id="post-1" data-bar="bar-1">...</div> then, said .audiojs element inside <div>, go this inside event handler <div> element (using .closest()) , value of it:
var barid = $(this).closest('[id^="post-"]').attr('data-bar'); then when need use it:
$("." + barid).each(function(i) {     fluctuate($(this)); }); 
Comments
Post a Comment