...

View Full Version : Button switch text and call different functions on onclick



JonesJ
12-22-2011, 10:15 PM
Hi,
This button changes the button text when clicked, but it is always calling the same function. How to make the button depending on the button text to call different functions on onclick?

[CODE]
<script type = "text/javascript">
function button_switch(){
if (document.switch_form.switch_b.value == "Adam"){
document.switch_form.switch_b.value="Eva";
}
else{
document.switch_form.switch_b.value = "Adam";
}
}
</script>

<form name = "switch_form">
<input type = "button" name = "switch_b" id = "switch_b" value = "Adam" onclick = "button_switch()" />
</form>
[CODE]

xelawho
12-22-2011, 10:21 PM
just add the function calls to your if/else...



<script type = "text/javascript">
function button_switch(){
if (document.switch_form.switch_b.value == "Adam"){
document.switch_form.switch_b.value="Eva";
one();
}
else{
document.switch_form.switch_b.value = "Adam";
two();
}
}

function one() {
alert("you clicked Adam")
}

function two() {
alert("you clicked Eva")
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum