badjuju badjuju - 15 days ago 7
HTML Question

setting object through variable containing strings not working

I have developed a pretty basic audio player on on my website similar to SoundClouds.

I have managed to successfully finish it, and I am starting to clean up all the markup, including (trying) removing all inline event handlers (i.e:

onclick=""
or
onkeyup=""
)
.

However, I have come across a bit of a problem in my JavaScript whilst doing this.

It's a bit hard to explain so I'm going to post my JavaScript, then give you an idea of what the problem is:

$(".track-w__trigger").click(function(){
var tid = $(this).attr('aria-trackid'); // aria-trackid is equal to a random integer created by PHP
var tiW = 'w' + tid + 'w'; // this is an object (i.e: w3w)
playPauseButton(this, tiW, ti);
});

function playPauseButton(button, wave, trackID){
var button = $(button);

if(wave.isPlaying()){ // the object (w3w.isPlaying())
button.removeClass("playing");
wave.pause();
} else {
button.addClass("playing");
wave.play();
}
}


What I used to have was

<div class="track-w__trigger" onclick="playPauseButton(this, w3w, 3)" id="w3w-trigger"></div>


and now it is:

<div class="track-w__trigger" aria-trackid="3" id="w3w-trigger"></div>


As you can see in the second block of code.
w3w
is an object. However, because I have set it using my
click
function in a separate
script
using string quotes. JavaScript has gone ahead and made it a
string
.

So now when I use
wave.isPlaying()
for example; it does nothing.

I have no idea how to fix this, and no result on Google will help me. Any help in fixing this issue would be greatly appreciated,

Thanks!

EDIT:

This is where & how
w3w
is set:

var w3w = Uki.start({
// e.t.c
});

Answer

Use eval wave = eval(wave); to evaluate the string as a function

Comments