473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

making onClick dynamic and functional


My task is to change the value of a button and then make it functional
with the onClick handler. Changing the value to "Play Again" works, but
making the onClick work accordingly does not. The following is a
snippet of the script. What is keeping it from working?

function displaycards1(){
document.form1.reveal1.value="Play Again";
document.form1.reveal1.onClick="setcards();";
}
</script>
<form name="form1">
<input id="reveal1" type="button" value="reveal"
onClick="which_clicked=this; displaycards1(); return false;">
</form>

I greatly appreciate it.
Thanks.
Edit/Delete Message

Jul 6 '06 #1
7 2764

ex********@yahoo.com wrote:
My task is to change the value of a button and then make it functional
with the onClick handler. Changing the value to "Play Again" works, but
making the onClick work accordingly does not. The following is a
snippet of the script. What is keeping it from working?

function displaycards1(){
document.form1.reveal1.value="Play Again";
document.form1.reveal1.onClick="setcards();";
}
</script>
<form name="form1">
<input id="reveal1" type="button" value="reveal"
onClick="which_clicked=this; displaycards1(); return false;">
</form>
I wouldn't change the onClick value at all. I'd set up the button this
way:
<input id="reveal1"..... value="reveal" onClick="doTask()">

function doTask() {
if (this.value=="reveal") {
this.value="Play Again";
doReveal();
} else {
this.value="reveal";
doPlayAgain();
}
}

Hope this helps. Stan

Jul 6 '06 #2
Another option is to create two handlers:

function revealHandler() {
this.value = "Play again";
this.onclick = playAgainHandler;
}

function playAgainHandler() {
this.value = "Reveal";
this.onclick = revealHandler;
}

function onload() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}

st*****@gmail.com wrote:
ex********@yahoo.com wrote:
>My task is to change the value of a button and then make it functional
with the onClick handler. Changing the value to "Play Again" works, but
making the onClick work accordingly does not. The following is a
snippet of the script. What is keeping it from working?

function displaycards1(){
document.form1.reveal1.value="Play Again";
document.form1.reveal1.onClick="setcards();";
}
</script>
<form name="form1">
<input id="reveal1" type="button" value="reveal"
onClick="which_clicked=this; displaycards1(); return false;">
</form>

I wouldn't change the onClick value at all. I'd set up the button this
way:
<input id="reveal1"..... value="reveal" onClick="doTask()">

function doTask() {
if (this.value=="reveal") {
this.value="Play Again";
doReveal();
} else {
this.value="reveal";
doPlayAgain();
}
}

Hope this helps. Stan
Jul 6 '06 #3

st*****@gmail.com wrote:
ex********@yahoo.com wrote:
My task is to change the value of a button and then make it functional
with the onClick handler. Changing the value to "Play Again" works, but
making the onClick work accordingly does not. The following is a
snippet of the script. What is keeping it from working?

function displaycards1(){
document.form1.reveal1.value="Play Again";
document.form1.reveal1.onClick="setcards();";
}
</script>
<form name="form1">
<input id="reveal1" type="button" value="reveal"
onClick="which_clicked=this; displaycards1(); return false;">
</form>

I wouldn't change the onClick value at all. I'd set up the button this
way:
<input id="reveal1"..... value="reveal" onClick="doTask()">

function doTask() {
if (this.value=="reveal") {
this.value="Play Again";
doReveal();
} else {
this.value="reveal";
doPlayAgain();
}
}

Hope this helps. Stan
The following code was entered and when I clicked on the the "Reveal"
button, the script went to the "setcards()" function, not recognizing
that the "Reveal" button was clicked.

function do_tasks(){
if (this.value=="Reveal") {
this.value="Play Again";
display_cards1();
} else {
this.value="reveal";
setcards();
}

}

<form name="form1">
<input name="reveal1" type="button" value="Reveal"
onClick="do_tasks(); return false;">
</form>

Jul 6 '06 #4

Vincent van Beveren wrote:
Another option is to create two handlers:

function revealHandler() {
this.value = "Play again";
this.onclick = playAgainHandler;
}

function playAgainHandler() {
this.value = "Reveal";
this.onclick = revealHandler;
}

function onload() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}
I tried the above with the following code, but it did not work, the
"Reveal" button did not change to "Play Again" or do anything else. I
added a document.write('testing'); and found that it worked, but the
rest of the code in the function did not get read at all or was not
interpreted properly. Replacing "this.value" with
"document.form1.reveal1.value="Play Again" did not do anything other
make the screen blank.

function reveal_handler(){
document.write("testing");
this.value="Play Again";
this.onClick=playAgainHandler();
displaycards1();
}
function playAgainHandler(){
this.value="Reveal Hand";
this.onClick=reveal_handler();
setcards();
}

<form name="form1">
<input name="reveal1" type="button" value="Reveal"
onClick="reveal_handler(); return false;">
</form>

Is "this.value" the proper code?
I appreciate your assistance.

Jul 6 '06 #5
Hi,

I made an error in the JS code:
>function onload() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}
should be:

window.onload = function() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}

This installs the revealHander 'event handler' on the 'onclick' event
after the page is loaded.


ex********@yahoo.com wrote:
Vincent van Beveren wrote:
>Another option is to create two handlers:

function revealHandler() {
this.value = "Play again";
this.onclick = playAgainHandler;
}

function playAgainHandler() {
this.value = "Reveal";
this.onclick = revealHandler;
}

function onload() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}

I tried the above with the following code, but it did not work, the
"Reveal" button did not change to "Play Again" or do anything else. I
added a document.write('testing'); and found that it worked, but the
rest of the code in the function did not get read at all or was not
interpreted properly. Replacing "this.value" with
"document.form1.reveal1.value="Play Again" did not do anything other
make the screen blank.

function reveal_handler(){
document.write("testing");
this.value="Play Again";
this.onClick=playAgainHandler();
displaycards1();
}
function playAgainHandler(){
this.value="Reveal Hand";
this.onClick=reveal_handler();
setcards();
}

<form name="form1">
<input name="reveal1" type="button" value="Reveal"
onClick="reveal_handler(); return false;">
</form>

Is "this.value" the proper code?
I appreciate your assistance.
Jul 7 '06 #6

Vincent van Beveren wrote:
Hi,

I made an error in the JS code:
>function onload() {
> // install first event on button
> document.getElementById('reveal1').onclick = revealHandler;
>}

should be:

window.onload = function() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}

This installs the revealHander 'event handler' on the 'onclick' event
after the page is loaded.


ex********@yahoo.com wrote:
Vincent van Beveren wrote:
Another option is to create two handlers:

function revealHandler() {
this.value = "Play again";
this.onclick = playAgainHandler;
}

function playAgainHandler() {
this.value = "Reveal";
this.onclick = revealHandler;
}

function onload() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}
I tried the above with the following code, but it did not work, the
"Reveal" button did not change to "Play Again" or do anything else. I
added a document.write('testing'); and found that it worked, but the
rest of the code in the function did not get read at all or was not
interpreted properly. Replacing "this.value" with
"document.form1.reveal1.value="Play Again" did not do anything other
make the screen blank.

function reveal_handler(){
document.write("testing");
this.value="Play Again";
this.onClick=playAgainHandler();
displaycards1();
}
function playAgainHandler(){
this.value="Reveal Hand";
this.onClick=reveal_handler();
setcards();
}

<form name="form1">
<input name="reveal1" type="button" value="Reveal"
onClick="reveal_handler(); return false;">
</form>

Is "this.value" the proper code?
I appreciate your assistance.
According to your suggestions, the following should work, but it does
not:

<html>
<body>
<script type="text/javascript">

window.onload = function() {
document.getElementById('reveal1').onclick =
reveal_handler();

}
function display1(){
document.write('test1');
}
function display2(){
document.write('test2');
}
function reveal_handler(){
this.value="Play Again";
this.onClick=playAgainHandler();
display1();
}
function playAgainHandler(){
this.value="Reveal Hand";
this.onClick=reveal_handler();
display2();
}
</script>
<form name="form1">
<input id="reveal1" type="button" value="Reveal">
</form>
</html>

What is missing?
Your assistance is appreciated.

Thanks.

Jul 7 '06 #7
Corrected version:

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

window.onload = function() {
document.getElementById('reveal1').onclick =
reveal_handler;

}
function display1(){
document.getElementById('placeholder').innerHTML = 'test1';
// You can not execute document.write when a page is already loaded
// use DOM instead.
}
function display2(){
document.getElementById('placeholder').innerHTML = 'test2';
}
function reveal_handler(){
this.value="Play Again";
this.onclick=playAgainHandler;
// JavaScript is case sensetive, the handler is onclick, not onClick
// and what you do is assign the reference to the function, not invoke
// it: this.onclick = playAgainHandler instead of
// this.onclick=playAgainHandler();
display2();
}
function playAgainHandler(){
this.value="Reveal Hand";
this.onclick=reveal_handler;
display1();
}
</script>
</head>
<body>
<form name="form1">
<input id="reveal1" type="button" value="Reveal">
</form>
<span id="placeholder"></span>
<!-- placeholder to show the message of display1 and display2 in -->
</body>
</html>

hope it helps! I tested it in FF and it works for me.

Good luck,
Vincent
ex********@yahoo.com schreef:
Vincent van Beveren wrote:
>Hi,

I made an error in the JS code:
> >function onload() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}

should be:

window.onload = function() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}

This installs the revealHander 'event handler' on the 'onclick' event
after the page is loaded.


ex********@yahoo.com wrote:
>>Vincent van Beveren wrote:
Another option is to create two handlers:

function revealHandler() {
this.value = "Play again";
this.onclick = playAgainHandler;
}

function playAgainHandler() {
this.value = "Reveal";
this.onclick = revealHandler;
}

function onload() {
// install first event on button
document.getElementById('reveal1').onclick = revealHandler;
}

I tried the above with the following code, but it did not work, the
"Reveal" button did not change to "Play Again" or do anything else. I
added a document.write('testing'); and found that it worked, but the
rest of the code in the function did not get read at all or was not
interpreted properly. Replacing "this.value" with
"document.form1.reveal1.value="Play Again" did not do anything other
make the screen blank.

function reveal_handler(){
document.write("testing");
this.value="Play Again";
this.onClick=playAgainHandler();
displaycards1();
}
function playAgainHandler(){
this.value="Reveal Hand";
this.onClick=reveal_handler();
setcards();
}

<form name="form1">
<input name="reveal1" type="button" value="Reveal"
onClick="reveal_handler(); return false;">
</form>

Is "this.value" the proper code?
I appreciate your assistance.

According to your suggestions, the following should work, but it does
not:

<html>
<body>
<script type="text/javascript">

window.onload = function() {
document.getElementById('reveal1').onclick =
reveal_handler();

}
function display1(){
document.write('test1');
}
function display2(){
document.write('test2');
}
function reveal_handler(){
this.value="Play Again";
this.onClick=playAgainHandler();
display1();
}
function playAgainHandler(){
this.value="Reveal Hand";
this.onClick=reveal_handler();
display2();
}
</script>
<form name="form1">
<input id="reveal1" type="button" value="Reveal">
</form>
</html>

What is missing?
Your assistance is appreciated.

Thanks.
Jul 8 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

34
by: Ian Rastall | last post by:
I'm doing a small project right now where I create a table from a database. This is the code, which Dreamweaver, for the most part, has written: *** <?php mysql_select_db($database_tdream,...
13
by: mr_burns | last post by:
hi, is it possible to change the contents of a combo box when the contents of another are changed. for example, if i had a combo box called garments containing shirts, trousers and hats, when...
1
by: Micromanaged | last post by:
I have files that are created with a dynamic name that I want to have hyperlinks created. Things work when I have a static name, but give me the 404 when I throw in the dynamic stuff. Here's...
10
by: Gernot Frisch | last post by:
Hi, I have found some menu functions. It works quite well, but how can I replace it with a simple <a href> if javascript is turned off? I reduced my code to:...
19
by: Christian Fowler | last post by:
I have a VERY LARGE pile of geographic data that I am importing into a database (db of choice is postgres, though may hop to oracle if necessary). The data is strictly hierarchical - each node has...
1
by: Todd Peterson | last post by:
I'm a newbie to DB2 and am trying to figure out how to write a stored procedure, using dynamic SQL statements to return a result set. I believe the majority of the hurdles I have been facing might...
3
by: Reinhold Schrecker | last post by:
Hi there, I am trying to generate a dynamic menu with JavaScript/DOM and have problems to set the onclick-attribute for my <a>-elements. The following code works fine in Opera and Mozilla but...
4
by: getsanjay.sharma | last post by:
Is there any way I can attach a function dynamically which takes some parameters ? <html> <head> <script> doSomething = function(x) { alert(x); }
11
by: julie.siebel | last post by:
I'm working on a rather complex booking system for building European trips, in a combination of SQL/VBScript/Javascript. There are tons of query string variables that get passed back and forth...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.