0

I am trying to write a PHP Java Script, but struggling to write in this section of coding.

I am trying to make a buttom in form in that opens

The code I have written so far is

function display(e){
    if (e.clicked)
        document.getElementById('2').style.display = 'none';
    else
        document.getElementById('2').style.display = 'block';

and the FORM CODE is;

 <input type="button" value=" Book Now " onClick="display(this)"/></input>

any help to point out my clear mistakes would be great, the live code can be seen at http://affordablecleaners.co.uk/quote/

Thanks,

Henry

2 Answers 2

2

Try something similar to the following

var i = 0;
var display = function() {
    document.getElementById('2').style.display = (i++ % 2) ? "none" : "block";
};

Essentially, we're creating a variable i and increasing it by one every time the function is called. If, when the function is called, i is an even number, then we set it to display: block. Otherwise, set it to display: none.

The biggest downside to this solution is cluttering the global scope. If this is an issue, you can also do the following.

var display = function() {
    document.getElementById('2').style.display = (document.getElementById('2').style.display == "none") ? "block" : "none";
};
Sign up to request clarification or add additional context in comments.

11 Comments

ooh, I like this, let me try it now
but wait, i think it needs to be ? "none" : "block"; as I want it to be hidden unless clicked
Oh, sorry. If you want it to be shown the first time it's clicked, change it to either i = 1 or just change the statements (? "none" : "block"). Updated
hmm.. what is my onclick= ??? do i need to assign a function name to the javescript?
onclick="display()", or assign this in the javascript.
|
0

Here's the (untested) logic...

function display(state, which){

if (state==1) {document.getElementById(which).style.display ='none';}

    else

    {document.getElementById(which).style.display = 'block';}
}

and then in your button...

to turn ON

onclick="display('1',someDIV)"

to turn OFF

onclick="display('0',someDIV)"

4 Comments

can you have a double on click for one button?
This one is a toggle but you can use the L-Click for ON and R-Click for OFF if you want. Would you like that?
I don't know how to handle double clicks if that's what you meant.
He wants to get rid of your state argument and have that be automatic within one click. He wants it to be an automatic toggle, basically... my script does that, though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.