Show / Hide Text Blocks Question

Hi everyone,

I have some text on the page arranged liked this:

Block 1 of text

Block 2 of text

Would someone be able to help me so that if you click on the title it shows the block of text underneath and if you click on the title again it hides the text?


Who is Participating?
Pratima PharandeConnect With a Mentor Commented:
try this

<title>my javascript show script</title>

<script type="text/javascript">
      function showStuff(id) {
      if (document.getElementById(id).style.display == 'block')
            document.getElementById(id).style.display = 'none';
            document.getElementById(id).style.display = 'block'


<p><a href="#" onclick="showStuff('answer1'); return false;">What price are your apples?</a><br>
<span id="answer1" style="display: none;">Our apples are 30 pence each. If you buy 10 or more we can sell them at a discounted bulk rate of 25 pence each.</span></p>

Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
TLN_CANADAAuthor Commented:
Thank you pratima, it works except when I put php in the output it does not hide on the page load:

<p><a href="#" onclick="showStuff('answer1'); return false;">Today:</a><br>
<span id="answer1" style="display: none;"><?php foreach ($res as $k => $v) {
    echo str_replace('__', $v, $html[$k]) . PHP_EOL;
} ?>

Open in new window

Do I need to put something around the php for it to work?


Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Pratima PharandeCommented:
try puttin simple PHP text only rather than Loop
TLN_CANADAAuthor Commented:
Tell you tell me how to print the results of the loop into another variable and then print this variable here as I need the results of the loop in this block. Thanks for your help :)
TLN_CANADAAuthor Commented:
Thanks, I have this figured out now.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.