Quesiton about two javascript functions, swtiching between them in ONCLICK

E

endlesstide

If I have two functions, and want to toggle back and forth between 1
and 2 from an onclick event.... how would I parse that? Thanks mucho



<script>

javascript function1() {

something1....

}

javascript function2() {

something2....

}

</script>
 
H

Hal Rosser

If I have two functions, and want to toggle back and forth between 1
and 2 from an onclick event.... how would I parse that? Thanks mucho



<script>

javascript function1() {
****change for demo
function functionOne(){
something1....

}

javascript function2() {
***change for demo
function functionTwo(){
something2....

}

</script>
One way is to add another function that will call the first two alternately
and your button calls the new function instead of the other two.:...
var booly = true; //(not in a function)
function thirdFunction(){//**(Call this from the button)
if (booly){
functionOne();
}else{
functionTwo();
}
booly != booly; // **changes booly from true to false or vice-versa
}//*end of function
HTH
 
T

therealview

Having some problems with this, check it:

<script>
var booly = true; //(not in a function)
function openinput(){//**(Call this from the button)
if (booly){
null;
}else{
alert('hi2');
}
booly != booly; // **changes booly from true to false or
vice-versa
}//*end of function


</script>
<a href="#" onclick=openinput()>Click</a>


this should in theory show "hi2" after clicking the link twice?
 
E

Evertjan.

wrote on 08 apr 2006 in comp.lang.javascript:
Having some problems with this, check it:

Please quote what you are replying to. If you want to post a followup via
groups.google.com, don't use the "Reply" link at the bottom of the
article. Click on "show options" at the top of the article, then click on
the "Reply" at the bottom of the article headers.
<script>
var booly = true; //(not in a function)
function openinput(){//**(Call this from the button)
if (booly){
null;
}else{
alert('hi2');
}
booly != booly; // **changes booly from true to false or
vice-versa
}//*end of function


</script>
<a href="#" onclick=openinput()>Click</a>


this should in theory show "hi2" after clicking the link twice?

No, because
1 you reload the page every time.
2 != is a comparison operator, not an assignment operator

Try:

<script type='text/javascript'>

var booly = false;

function myTest(){
if (booly)
alert('hi2');
booly = !booly;
return false;
}
</script>

<a href="#" onclick='return myTest()'>Click</a>
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Sat, 8 Apr 2006 02:27:54 remote, seen in Hal
Rosser said:
One way is to add another function that will call the first two alternately
and your button calls the new function instead of the other two.:...
var booly = true; //(not in a function)
function thirdFunction(){//**(Call this from the button)
if (booly){
functionOne();
}else{
functionTwo();
}
booly != booly; // **changes booly from true to false or vice-versa
}//*end of function
HTH

Your code is unlikely to help anybody until you learn
(a) javascript,
(b) how to test code before posting it.

There is no assignment operator != (it is relational), and the line
containing it has no effect.

Consider, however

var B = true
...
(B = !B) ? F1() : F2()
or ((B = !B) ? F1 : F2) ()
 
H

Hal Rosser

****change for demo
function functionOne(){
***change for demo
function functionTwo(){
One way is to add another function that will call the first two alternately
and your button calls the new function instead of the other two.:...
var booly = true; //(not in a function)
function thirdFunction(){//**(Call this from the button)
if (booly){
functionOne();
}else{
functionTwo();
}
booly != booly; // **changes booly from true to false or vice-versa
}//*end of function
HTH

I don't believe I did that -
Change code from
booly != booly; //** incorrect comparison
to
booly = !booly; //**(correct assignment)
*** duh
 
H

Hal Rosser

Your code is unlikely to help anybody until you learn
(a) javascript,
(b) how to test code before posting it.

There is no assignment operator != (it is relational), and the line
containing it has no effect.
**** I caught it - but too late for your barb.
Consider, however

var B = true
...
(B = !B) ? F1() : F2()
or ((B = !B) ? F1 : F2) ()

Clever! Impresses me, but probably confuses the OP.
y'made me look at
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Operators:Logical_Operators
for an explanation. thanky, thanky - keep it coming -
oh, and I will also persist due to your encouragement . :)
 
H

Hal Rosser

Having some problems with this, check it:

<script>
var booly = true; //(not in a function)
function openinput(){//**(Call this from the button)
if (booly){
null;
}else{
alert('hi2');
}
booly != booly; // **changes booly from true to false or
vice-versa
}//*end of function


</script>
<a href="#" onclick=openinput()>Click</a>


this should in theory show "hi2" after clicking the link twice?
Sorry - change my code FROM "booly != booly TO "booly = !booly
I would have caught it if I tested it first -
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top