Username:
Password:
Remember me:
Register

Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)


Search forums via Google


0 Users appreciate this thread.

Randomizing a button's function
Started by squirtle2016
(2016-02-14 19:16:20)
squirtle2016 (2016-02-14 19:16:20)
I need to know how to make a button have a different onClick event than a series of buttons, and how to randomly choose that button at the time the page loads. Can you help?

This post has been edited one or more times, the last time was:
2016-02-14 19:16:54

Stop, or you die.
DoctorSatan (2016-02-18 18:39:41)
If you post a detailed description of what you want, or what you're trying to accomplish with the code, I can write the JavaScript for you. I don't really know what you're trying to achieve and can't properly help you unless I do.
squirtle2016 (2016-02-18 18:55:19)
I have an array of buttons. I set two functions, one for a single button, called function1, and another for the rest, called function2. I want to know how to have the page randomly select the one button that has function1 applied, while the rest have function2 applied.

This is for a game on my site.

This post has been edited one or more times, the last time was:
2016-02-18 18:56:08

Stop, or you die.
DoctorSatan (2016-02-18 20:07:13)
I'm still not sure what you're trying to accomplish, but based on the information you've given me, using one function for everything makes more sense. So I am assuming you have something along the lines of:

function one() {
// do something
}

function two() {
// do something else
}

Why not use function parameters to split up what you want to achieve based on the button clicked?

function test(button) {
if(button == 1) {
//do something
}

else {
//do something else
}
}

If you insist on doing it the way you described, you can change the ID attribute of the button whenever the function is called on it (via onclick or whatever) to signify that it has that function applied, then remove that ID from any other div with the ID, but to me it seems like a lot of work to fix a simple problem.

What exactly are you trying to code? If it's hosted publicly, a link would be appreciated.

This post has been edited one or more times, the last time was:
2016-02-18 20:08:15

squirtle2016 (2016-02-19 21:01:45)
I'm trying to code a game on my website. But I'm not sure if it's even going to be public at all.
Stop, or you die.
NodePoint (2016-02-22 18:12:25)
If it's on your website and people can visit it then it's considered publicly accessible.
Security researcher, web developer, artist, and tech enthusiast.
squirtle2016 (2016-02-24 00:40:17)
^ The URL is long and confusing, like you, and it hasn't been linked to.
Stop, or you die.
squirtle2016 (2016-02-24 00:41:17)
I just found it out! I'll close it for that reason.
Stop, or you die.
 

This topic is closed, so you can not post a comment.

This topic's ID: 82151

Back to forum: Web Programming (HTML, JS, CSS, PHP, MySQL)




Total registered users: 8321
New registered users today: 7
Newest registered user: ElegantVulpes

©  Copyright 2026 3DSPlaza. All Rights Reserved