PDA

View Full Version : Associating a script with an onClick event of a


crubbles
12-31-2002, 02:37 AM
This is a script I made. Only problem is, that when the page loads..the script executes. I want to associate the execution of my script with the onClick of the button.

If my script needs to be totally revamped because I started off totally wrong...please don't tell me that. Instead call me an idiot and tell me to give up trying to program altogether. Thanks. :)

<p>
<form name=solve>
<input type="button" value="Fix my problem" onClick="solve()">


<SCRIPT LANGUAGE="JavaScript">


var solve;
var solvey;
var solveyy;
var solveyn;
var solveyyy;
var solveyny;
var solveynyy;

solve = prompt("Has something gone wrong?", "Type yes or no");

if(solve =='no') {
alert('Then DONT mess with it!!');
alert('Problem Solved!');

// *************************************************
// this is as far as anyone willing to help needs to see --I think :P

}
if(solve == 'yes') {
solvey = prompt("Was it your fault?",'');

if(solvey == 'yes') {
alert("You fool!");
solveyy = prompt("Does anyone know?",'');

if(solveyy == 'yes') {
alert("You got it tuff..");
solveyyy = prompt("Can you blame someone else?",'');

if(solveyyy == 'yes') {
alert("Do so quickly");
alert("Problem Solved!");
}
else {
alert("You got it bad... stiff it out!");
alert("Problem Solved!");
}
}
else {
alert("Hide it!");
alert("Problem Solved");
}
}
else {
solveyny = prompt("Will you get in trouble anyway?",'');
if (solveyny == 'yes') {
solveabaa= prompt("Can you blame someone else?",'');
if(solveynyy == 'yes') {
alert("Problem Solved!");
}
else {
alert("You're in deep...stiff it out!");
alert("Problem Solved!");
}
}
else {
alert("Make sure there are no witnesses!!");
alert("Problem Solved!");
}
}
}

// I saw a sort of flowsheet drawing of this on the internet
// And wanted to see if I could duplicate it in JavaScript
// It's pretty funny; you can use it on your own site, as long
// as you don't claim you built it.
// Do not steal without permission!!
// I know its written very sloppy, so if you improve on it...please
// email crubbles@hotmail.com with the source. Thanks.


</SCRIPT>

</form>

joeframbach
12-31-2002, 03:33 AM
you actually thought you did something wrong?

<p>
<form name=solve>
<input type="button" value="Fix my problem" onClick="solve()">


<SCRIPT LANGUAGE="JavaScript">

function solve(){
var solve;
var solvey;
var solveyy;
var solveyn;
var solveyyy;
var solveyny;
var solveynyy;

solve = prompt("Has something gone wrong?", "Type yes or no");

if(solve =='no') {
alert('Then DONT mess with it!!');
alert('Problem Solved!');

// *************************************************
// this is as far as anyone willing to help needs to see --I think :P

}
if(solve == 'yes') {
solvey = prompt("Was it your fault?",'');

if(solvey == 'yes') {
alert("You fool!");
solveyy = prompt("Does anyone know?",'');

if(solveyy == 'yes') {
alert("You got it tuff..");
solveyyy = prompt("Can you blame someone else?",'');

if(solveyyy == 'yes') {
alert("Do so quickly");
alert("Problem Solved!");
}
else {
alert("You got it bad... stiff it out!");
alert("Problem Solved!");
}
}
else {
alert("Hide it!");
alert("Problem Solved");
}
}
else {
solveyny = prompt("Will you get in trouble anyway?",'');
if (solveyny == 'yes') {
solveabaa= prompt("Can you blame someone else?",'');
if(solveynyy == 'yes') {
alert("Problem Solved!");
}
else {
alert("You're in deep...stiff it out!");
alert("Problem Solved!");
}
}
else {
alert("Make sure there are no witnesses!!");
alert("Problem Solved!");
}
}
}

// I saw a sort of flowsheet drawing of this on the internet
// And wanted to see if I could duplicate it in JavaScript
// It's pretty funny; you can use it on your own site, as long
// as you don't claim you built it.
// Do not steal without permission!!
// I know its written very sloppy, so if you improve on it...please
// email crubbles@hotmail.com with the source. Thanks.
}

</SCRIPT>

</form>

crubbles
12-31-2002, 04:30 AM
Yes actually. The script itself works as it should. However I wish to change the call from executing onLoad to onClick of the input button.

I am sorry if I did not make that clear in my original question as English is not my first language.

Algorithm
12-31-2002, 06:43 AM
joe gave you the solution to your problem, though he wasn't very clear in showing it. What you need to do is surround your code with a function declaration, as follows:

function solve(){

// Code goes here

}

Apart from that, you're fine. :)

crubbles
12-31-2002, 03:56 PM
I'm not so sure. Whenever I run the code
(even with the: function solve(){ script } ...I still get a runtime error telling me that the object does not support the property or method. I think the problem is with the:

<form name=solve>
<input type="button" value="Fix my problem" onClick="solve()">

duniyadnd
12-31-2002, 05:23 PM
Crubbles, the two lines of code you posted in your last post is actually where the problem lies.

<form name=solve>
<input type="button" value="Fix my problem" onClick="solve()">

The problem lies in the word "solve"

You can't have it to identify a form and as a seperate method, as they both becomes a way to be referenced, and if they have the same name, clicking on onClick looks for object solve, which is the form name.

Change one of their names from the code joeframbach gave you, as you still have to set up the function solve().

Duniyadnd

crubbles
12-31-2002, 06:05 PM
Excellent, Thank you much. It works perfectly now.