1

Could anyone tell me how I can use this code to show and hide data between DIV`s (not to hide and show - I want the other way)?

I`m using in .js file:

function toggle(sDivId) {
                var oDiv = document.getElementById(sDivId);
                oDiv.style.display = (oDiv.style.display == "none") ? "block" : "none";
}

And in .php file:

<div onclick="toggle('divContent1')" style="cursor: pointer;">Hide and show</div>
    <div id="divContent1">
    text here
    </div>
</div>

This code is working just fine but when the page is loading I wish to have text already hidden (not after when I click "Hide and show" text).

Thanks!

1
  • That's not jquery, and you can just hide the element with CSS or call your function on page load Commented Sep 21, 2013 at 18:02

7 Answers 7

1
$('#clickMe').click(function(){
 $('#divContent1').toggle();

});

Fiddle Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Hey Thanks for acceptence .Please upvote and accepet as answer
1

I may have misunderstood your question, but if you want the text hidden on page load can't you just stick a display:none tag, as follows:

<div id="divContent1" style="display: none">
text here
</div>

Comments

1

You can use the code you have but just add display:none; in the css. Or in the php inside the style="display:none;"

<div onclick="toggle('divContent1')" style="cursor: pointer;">Hide and show</div>
    <div id="divContent1" style="display:none;">
    text here
    </div>
</div>

jQuery has also a toogle() function you could use.

demo

Comments

1

The code you provided isn't using jquery. If you'd like to do this using jquery you can change things to something like this.

function toggle(sDivId) {
    $("#"+sDivId).toggle();
}

and to your php block you'll want to add display:none; to initially hide the inner div

<div onclick="toggle('divContent1')" style="cursor: pointer;">Hide and show</div>
    <div id="divContent1" style='display:none'>
    text here
    </div>
</div>

Comments

0

You can use toggle() function to alternately switch between show() and hide(). Otherwise, use the show() and hide() functions directly.

Comments

0

call the function toggle('divContent1') at onload event of the body

<body onload="toggle('divContent1')">

Comments

0

Use this code :

function load(){
   document.getElementByID('divContent1').style.display = 'none';
}

and in your html

<body onLoad = "load()">

Comments

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.