Click to See Complete Forum and Search --> : Depressable Button Problem!
WWright
02-16-2003, 01:40 AM
Hi there, I'm new to Javascript. Here's my problem: I've made a drepessable button but I want it to display text on the same page each time I press it so I can use the button as many times as I want. If I use document.write it erases the button after the first click. How can I fix this?
<head>
var r = 0;
var i = 0;
function RandomName()
{
r = Math.floor(Math.random() * lastname.length);
i = Math.floor(Math.random() * malename.length);
document.write(malename[i]+" "+lastname[r])
}
</head>
<body>
<a href="#" onMousedown="document.images['example'].src=img2.src"
onClick="RandomName()" onMouseup="document.images['example'].src=img1.src">
<img src="picture.gif" name="example" border=0>
</body>
Nevermore
02-16-2003, 05:10 AM
Do you want it to add more text with each click, or delete the first lot of text each time?
Nevermore
02-16-2003, 05:12 AM
This code should replace the text each time:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
var r = 0;
var i = 0;
function RandomName()
{
r = Math.floor(Math.random() * lastname.length);
i = Math.floor(Math.random() * malename.length);
name = (malename + " " + lastname);
document.getElementById("text").firstChild.nodeValue=name;
}
</head>
<body>
<a href="#" onMousedown="document.images['example'].src=img2.src"
onClick="RandomName()" onMouseup="document.images['example'].src=img1.src">
<img src="picture.gif" name="example" border=0>
</body>
</html>
WWright
02-16-2003, 03:31 PM
I'm not sure I understand your fix, basically what GetElemenById is. It was giving me an error on page when I clicked the button. Here is a more complete version of the javascript that I want to generate random names with replacing the text everytime but keeping the depressable button:
<html>
<head>
<Script language="JavaScript">
var malename = new Array(
"Allen","Bob","Gerald")
var lastname = new Array(
"Adams","Bowden","Prichard")
var r = 0;
var i = 0;
function RandomName()
{
r = Math.floor(Math.random() * lastname.length);
i = Math.floor(Math.random() * malename.length);
name = (malename[i]+" "+lastname[r]);
document.getElementById("text").firstChild.nodeValue=name;
}
img1=new Image()
img1.src="picture.gif"
img2=new Image()
img2.src="picture.gif"
// End -->
</script>
</HEAD>
<body>
<a href="#" onMousedown="document.images['example'].src=img2.src"
onClick="RandomName(this.form)" onMouseup="document.images['example'].src=img1.src">
<img src="cursor.gif" name="example" border=0></a>
</body>
</html>
Nevermore
02-16-2003, 03:39 PM
Sorry, completely my fault. I left out part of the script (woops!). Here is a working version that should solve your problems:
<html>
<head>
<Script language="JavaScript">
var malename = new Array(
"Allen","Bob","Gerald")
var lastname = new Array(
"Adams","Bowden","Prichard")
var r = 0;
var i = 0;
function RandomName()
{
r = Math.floor(Math.random() * lastname.length);
i = Math.floor(Math.random() * malename.length);
name = (malename[i]+" "+lastname[r]);
document.getElementById("text").firstChild.nodeValue=name;
}
img1=new Image()
img1.src="picture.gif"
img2=new Image()
img2.src="picture.gif"
// End -->
</script>
</HEAD>
<body>
<p ID="text"> </p>
<a href="#" onMousedown="document.images['example'].src=img2.src"
onClick="RandomName(this.form)" onMouseup="document.images['example'].src=img1.src">
<img src="cursor.gif" name="example" border=0></a>
</body>
</html>
WWright
02-16-2003, 04:28 PM
It still gives me an error on page see:
example (http://www.geocities.com/elektrum2001/index.htm)
WWright
02-16-2003, 08:14 PM
so no fix?
Nevermore
02-17-2003, 02:54 AM
I see what's happened. For it to work you need some text in the <p> tags. When I posted, th forum has interpreted the code for a space as a space, and so hasn't displayed it, so the code didnt work. Add it and it should run fine. I can't post the code for a space, of course, because it just makes a space. The code is these letters joined together: (just insert them between the paragraph tags) )& n b s p ; (Including semicolon). Sorry about that!
WWright
02-17-2003, 12:56 PM
Thank you so much, it worked. I had to use alt+0160 for the space tho.