html - Javascript execute function from click -
i have these 3 function apply dry principles too.
js
function shakebell(){ document.getelementbyid('shakebell').play(); } function shakeshake() { document.getelementbyid('shakeshake').play(); } function blowwhistle(){ document.getelementbyid('blowwhistle').play(); } html
<audio id="shakebell" src="audio/bell-ringing-02.mp3" preload="auto"></audio> <audio id="shakeshake" src="audio/pill-bottle-1.mp3" preload="auto"></audio> <audio id="blowwhistle" src="whistle-flute-2.mp3" preload="auto"></audio> <div class="imagelist"> <a href="javascript:shakebell();"> <img src="images/golden_bell.png" style="width:100px; height 100px;"></a> <img src="images/maracas.png" style="width:100px; height:100px;"> </div> i have multiple images when clicked plays specific sound file picture. how can without making separate functions each image?
you can single function 1 parameter, like:
function shake(id){ document.getelementbyid(id).play(); } then call it:
<a href="javascript:shake('shakebell');"> <img src="images/golden_bell.png" style="width:100px; height 100px;"></a>
Comments
Post a Comment