Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-22-2011, 10:15 PM   PM User | #1
JonesJ
New Coder

 
Join Date: Jul 2011
Posts: 36
Thanks: 15
Thanked 0 Times in 0 Posts
JonesJ is an unknown quantity at this point
Button switch text and call different functions on onclick

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]
JonesJ is offline   Reply With Quote
Old 12-22-2011, 10:21 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
just add the function calls to your if/else...

Code:
<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>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
JonesJ (12-22-2011)
Reply

Bookmarks

Tags
button change text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:17 AM.


Advertisement
Log in to turn off these ads.