Nested selection
The HTML:
<p><button id="cow">Cow</button></p>
<p><button id="pig">Pig</button></p>
<p><button id="duck">Duck</button></p>
The JavaScript code:
function showSound() {
var animal = this.id;
if (animal == 'cow') {
alert('Mooo');
} else {
if (animal == 'pig') {
alert('Oink');
} else {
if (animal == 'duck') {
alert('Quack');
}
}
}
}
function init() {
document.getElementById('cow').onclick = showSound;
document.getElementById('pig').onclick = showSound;
document.getElementById('duck').onclick = showSound;
}
window.onload = init;