/    Sign up×
Community /Pin to ProfileBookmark

dual onClick states (on/off) for multiple "buttons"?

Hey all, I’ve got a set of panels that highlight when you scroll over them, and are meant to stay highlighted after clicking them.. then, after clicking them a second time, they are meant to go back to their original state.

I have it working fine with one panel, but the way it’s set up now, the same on/off variable (inUse) is affected by clicking any of the panels, which screws it up if you want to turn more than one “on” at a time.

I’m thinking I need to make an array to set up a corresponding variable for each of the panels, but I can’t quite figure out how to do it. Any suggestions?

The relevant code is

[code=php]var inUse = -1;

function flip() {
inUse = inUse * (-1)
}

function on (location, state) {
if (inUse == -1) {
document.images[swap_image[location]].src = button[state].src;
}
}

function off (location, state) {
if (inUse == -1) {
document.images[swap_image[location]].src = button[state].src;
}
} [/code]

with

[code=php]<body>
<div ID=”panel_1″>
<span onMouseOver=”(on(0,1))”, onClick=”(flip())”, onMouseOut=”(off(0,0))”></span></a>
</div>
<div ID=”panel_2″>
<span onMouseOver=”(on(1,3))”, onClick=”(flip())”, onMouseOut=”(off(1,2))”></span></a>
</div>
<div ID=”panel_3″>
<span onMouseOver=”(on(2,5))”, onClick=”(flip())”, onMouseOut=”(off(2,4))”></span></a>
</div>
<div ID=”panel_4″>
<span onMouseOver=”(on(3,7))”, onClick=”(flip())”, onMouseOut=”(off(3,6))”></span></a>
</div>[/code]

Thanks in advance!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@7studApr 26.2005 — Hi,

You could attach your on/off flag directly to the element. By the way, this notation:

onClick="(flip())"

is frivolous unless you enjoy typing parentheses. Use this notation instead:

onClick = "flip()"

To attach your on off/flag to the html element, you need to pass the 'this' keyord to your function:

onClick = "someFunc(this)"

Inside an html element, 'this' is a reference to the element. So, if you pass the this keyword to the function, it will have a reference to the html element that was clicked. Then, in your function you can do this:
[CODE]function someFunc(elmt)
{

elmt.inUse = true;

//or

elmt.inUse = false

...
...
}[/CODE]


You can read the value doing something like this:

if(elmt.inUse) //do something

or you can flip the switch like this:

elmt.inUse = !elmt.inUse
Copy linkTweet thisAlerts:
@MalkalypseauthorApr 26.2005 — Sorry, I'm still a little confused. I changed my elements as thus

<span onMouseOver="on(0,1)", onClick="flip(this)", onMouseOut="off(0,0)">

but as for the the function, not sure what goes where. Right now I have it set up as

function flip(elmt)

{

elmt.inUse = elmt.inUse * (-1);

alert (inUse);

}

and the alert always calls up the my default setting value for inUse. How do I make the "elmt" in the function understood at the "this" in the element?
Copy linkTweet thisAlerts:
@MalkalypseauthorApr 27.2005 — Hey, I've been playing with an example I found online and came across something really bizarre. I don't know what is up with this. After getting rid of everything but the meat and examining what made it tick, the result was the attached document.

When I changed the variable to read one of the gifs from my locally stored copy instead of the one on the site, it caused the function that called on it to stop working. I cannot for the life of me figure out why this would be the case???

Also, I'm still working on the previous problem, as I've not been able to put the pieces together correctly with 7's solution (still learning this stuff). I think this tutorial could be exactly what I need, but what's up with this whacked out gif?

[upl-file uuid=792adf6e-7a64-4c42-a652-afb50c84c0a2 size=1kB]Toggle.zip[/upl-file]
Copy linkTweet thisAlerts:
@7studApr 27.2005 — elmt.inUse = true;

//or

elmt.inUse = false[/QUOTE]

elmt.inUse = elmt.inUse [COLOR=Red]* (-1)[/COLOR];[/QUOTE]
or you can flip the switch like this:

elmt.inUse = [COLOR=blue][B]![/B][/COLOR]elmt.inUse

[/QUOTE]

------



alert ([COLOR=Red]inUse[/COLOR]);[/QUOTE]

alert([COLOR=green]elmt.inUse[/COLOR]);
-----


onMouseOut="off([COLOR=Red]0,0[/COLOR])"[/QUOTE]
onMouseOut="off([COLOR=Green]this,[/COLOR] 0)"

function off ([COLOR=Green]elmt[/COLOR], location)
Copy linkTweet thisAlerts:
@MalkalypseauthorApr 27.2005 — okay, you can (hopefully) see the problems I'm having implementing what you're telling me, and maybe show me the correct way to do it, looking here.

http://www.jonesmfgmusic.com/testpage_setup/pedals/pedals.html

http://www.jonesmfgmusic.com/testpage_setup/pedals/pedals2.html

I can't leave the state out of the on and off function, I'm not sure if you intended me to or not or if you were just shoing me that you were putting the elmt in. Also, I still don't know where the elmt.inUse = false; is supposed to go. If I put it in my flip function, then it will always return the same value, and if I put it anywhere else, it won't be called.

I get the impression that you know what you're talking about, but I'm just not understanding you properly. You might be assuming I know some of this stuff off the top of my head, but it's still new to me.
×

Success!

Help @Malkalypse spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...