I have these four buttons and I need them to display an alert message depending on which button is clicked. I'm new at programming and am trying to get this function to work. But nothing happens when I click the buttons. I'm not sure whether I'm not applying the onclick event properly. I don't want to insert it in the html markup. Could anyone help me? What I'm I doing wrong here?
This is my html and Js code:
<body>
<ul style="list-style-type:none" id="buttons">
<li><input type="button" value="English" id="english"/></li>
<li><input type="button" value="Spanish" id="spanish"/></li>
<li><input type="button" value="Hebrew" id="hebrew"/></li>
<li><input type="button" value="French" id="frech"/></li>
</ul>
<script language="javascript" type="text/javascript" src="lab6functions.js">
</script>
</body>
function buttonClick () {
if(!document.getElementsByTagName) return false;
if(!document.getElementById) return false;
//store buttons in variable buttonsList
var buttonsList = document.getElementsByTagName("input");
//iterate through all elements of buttonList
for (var i = 0; i<buttonsList.length; i++) {
//store each element to the variable buttons and get each individual id for each button
var buttons = buttonsList[i].getElementById("id");
//when a button is clicked, display the alert box with the message corresponding to each language
switch(buttons) {
case "english":
alert("Hello! How are you?");
break;
case "spanish":
alert("Hola! Como estas?");
break;
case "hebrew":
alert("Shalom!");
break;
case "french":
alert("Bonjour!");
break;
default:
alert("Please select a language");
}
buttons.onclick = buttonClick();
}
}