/    Sign up×
Community /Pin to ProfileBookmark

trying to "cycle" the value of a variable

Hi I’m trying to write some code to cycle the value of my universal variable window.BodyPart (BodyPart) being passed to the function when it’s called (onclick event of imagemap). For example onclick=AddMassinst(‘shoulder’), then using window.shoulder by refering to it through window.BodyPart (is that right or is my syntax all messed, or is that even possible?)

Here’s my code, I’m expecting window.BodyPart to cycle through blank, a, b, c, then blank (undefined) again. I’m very new to this so please forgive my ignorance.

function AddtoMassInst(BodyPart)

{
alert(window.BodyPart)

if (window.BodyPart=”)

{
window.BodyPart=’a’
}

else if (window.BodyPart=’a’)

{
window.BodyPart=’b’
}

else if (window.BodyPart=’b’)
{

window.BodyPart=’c’
}

else if (window.BodyPart=’c’)
{
window.BodyPart=”
}
else
{
alert(“else!”)
}

return true;
}

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Oct 23.2011 — Your problem is that "==" is the equality operator, and "=" is the assignment operator. So "if (window.BodyPart == 'c')" is what you want.

I'm not exactly sure what you mean, so if you've further problems, post again.
Copy linkTweet thisAlerts:
@vwphillipsOct 23.2011 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/
var BodyPart='';

function AddtoMassInst(){
alert(BodyPart)

if (BodyPart[COLOR="Red"]==[/COLOR]''){
BodyPart='a'
}

else if (BodyPart=='a'){
BodyPart='b'
}

else if (BodyPart=='b'){

BodyPart='c'
}

else if (BodyPart=='c'){
BodyPart=''
}
else{
alert("else!")
}

return true;
}
/*]]>*/
</script></head>

<body>
<input type="button" name="" value="Test" onmouseup="AddtoMassInst();" />
</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@Michael_BlissauthorOct 23.2011 — thanks that helped a lot! I still have one unsolved problem: what I'm trying to do is setup a global variable (I know they're bad, is there another simple way of doing this?) for each bodypart clicked on an anatmical image. The variable would contain a number of possible states that are cycled through (you helped me accomplish this already). What I want to know is do I have to declare each variable seperately outside the function, or can I pass the name of the variable via the function call (so onClick="AddBodyPart('Neck' )" or onClick="AddBodyPart('Face') would each generate their own variable window.Neck and window.Face with this code) for example?

function AddBodyPart(BodyPart)

{

alert(window.BodyPart)

if (window.BodyPart==undefined)

{

window.BodyPart=BodyPart+'a'

}

else if (window.BodyPart==BodyPart+'a')

{

window.BodyPart=BodyPart+'b'

}

else if (window.BodyPart==BodyPart+'b')

{

window.BodyPart=BodyPart+'c'

}

else if (window.BodyPart==BodyPart+'c')

{

window.BodyPart=undefined

}

else

{

alert("else!")

}

return true;

}
Copy linkTweet thisAlerts:
@JunkMaleOct 23.2011 — anything not declared as a [COLOR="Navy"][B]var[/B][/COLOR] is global.
Copy linkTweet thisAlerts:
@Declan1991Oct 23.2011 — I still don't get what you are trying to do, but you can certainly do this.function addBodyPart(bodyPart) {
if (window[bodyPart] == undefined) {
window[bodyPart] = bodyPart + "a";
}
else if (window[bodyPart] == bodyPart + "a") {
window[bodyPart] = bodyPart + "b";
... }
}


This can also be done with closures, but if I don't know what you are doing this for, I can't really show a good (i.e. appropriate) example of that.
Copy linkTweet thisAlerts:
@vwphillipsOct 24.2011 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">
/*<![CDATA[*/

function AddtoMassInst(bp){
var temp=window[bp]||'';

if (temp==''){
temp='a'
}

else if (temp=='a'){
temp='b'
}

else if (temp=='b'){

temp='c'
}

else if (temp=='c'){
temp=''
}
else{
alert("else!")
}
window[bp]=temp;
alert(BodyPart)
return true;
}
/*]]>*/
</script></head>

<body>
<input type="button" name="" value="Test" onmouseup="AddtoMassInst('BodyPart');" />
</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@Logic_AliOct 24.2011 — This function will return the next value in the array [FONT=Courier New]series[/FONT] if it finds [FONT=Courier New]current[/FONT] within the array, otherwise returns "".

[FONT=Courier New]myVar[/FONT] does not have to be global.

<i>
</i>var myVar = 'cranium' ;

function getNextValue( current, series )
{ <br/>
for( var i = 0, len = series.length; i &lt; len &amp;&amp; series[ i ] !== current; i++ )
;

return i == len || i == series.length -1 ? "" : series[ i + 1 ];
}

myVar = getNextValue( myVar, [ 'mandible', 'metatarsal', 'clavicle', 'cranium', 'femur', 'vertabra' ] );

alert( myVar );
×

Success!

Help @Michael_Bliss 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 5.25,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...