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 02-08-2012, 04:03 AM   PM User | #1
BirdIsTheWord
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
BirdIsTheWord is an unknown quantity at this point
problem with accessing onclick attribute

What I am trying to achieve, is a button that calls a function that changes the onclick attribute of that button, so that when it is clicked again, it resets the onclick attribute to call the default function. here is what the stripped down code looks like:
Code:
<html>
<head>
<script type="text/javascript">
function remove_prep(a){
a.value="cancel";
a.onclick="cancel_remove(this);";
}
function cancel_remove(a){
a.value="remove";
a.onclick="remove_prep(this);";
}
</script>
</head>
<body>
<input type="button" value="remove" onclick="remove_prep(this);" />
</body>
</html>
for some reason, it only fires once, after which it ceases to work properly.


thanks for the help this part of my code is killing me.

Last edited by BirdIsTheWord; 02-08-2012 at 04:29 AM..
BirdIsTheWord is offline   Reply With Quote
Old 02-08-2012, 04:19 AM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you want it to be able to flip back and forth infinitely?
Code:
<html>
<head>
<script type="text/javascript">
var clicked=false;
function duck(){
alert("wabbit season")
clicked=true;
}
function wabbit(){
alert("duck season")
clicked=false;
}
</script>
</head>
<body>
<input type="button" value="click me!" onclick="clicked==false?duck():wabbit()" />
</body>
</html>
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
BirdIsTheWord (02-08-2012)
Old 02-08-2012, 04:23 AM   PM User | #3
BirdIsTheWord
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
BirdIsTheWord is an unknown quantity at this point
yes, this is great, thank you!
BirdIsTheWord is offline   Reply With Quote
Old 02-08-2012, 04:31 AM   PM User | #4
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
you're welcome. there's actually lots of ways to do this. here's another which is a bit closer to what you were trying:

Code:
<html>
<head>
<script type="text/javascript">

function duck(btn){
alert("wabbit season")
btn.onclick=function(){wabbit(btn)}
}
function wabbit(btn){
alert("duck season")
btn.onclick=function(){duck(btn)}
}
</script>
</head>
<body>
<input type="button" value="click me!" onclick="duck(this)" />
</body>
</html>
xelawho is offline   Reply With Quote
Old 02-08-2012, 04:36 AM   PM User | #5
BirdIsTheWord
New to the CF scene

 
Join Date: Feb 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
BirdIsTheWord is an unknown quantity at this point
thank you, seriously I've been puzzled over why it didn't work for the past two days, now I can focus on other things, like sleep
BirdIsTheWord is offline   Reply With Quote
Old 02-08-2012, 06:59 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
just remember that the HTML event attributes are not the same as the JavaScript event properties. the former require a parseable JavaScript expression/statement, while the latter require a function. there are also differences in the used scope and automatically passed parameters.

PHP Code:
function test()
{
    
alert(this); // show scope
    
alert(arguments[0]); // show 1st passed parameter

PHP Code:
<button type="button" id="a_button" onclick="test();">click me</button>
<
button type="button" id="another_button">click me</button>
<
script type="text/javascript">
document.getElementById("another_button").onclick test;
</script> 
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
attribute, change, event, javascript, onclick

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 01:34 AM.


Advertisement
Log in to turn off these ads.