Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: pass variable to arrow function instead of setting harcoded value

How to replace value 1 by the variable x when setting an onclick function to a td like below:

var sounds = [1, 3, 8, 12];
var x = 123;
mytd.onclick = () => play(sounds[1].toString());

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Perhaps you want "currying"?

var sounds = [1, 3, 8, 12];
const playSound = (snd) => () => play(sounds[snd].toString());

var x = 123;
mytd.onclick = playSound(x)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading