0

I am trying to code a simple click to read more within a paragraph. I am sure this code worked to both show AND hide on click. When I try this now, it shows the hidden text, but will not hide it when I click again?

<div onclick="document.getElementById ('review').style.display=document.getElementById

('review').style.display='none' ? 'block':'none';">

<p>Click here to read our review</p>

<div id="review" style="display: none;">Review text goes here.</div>  

Any ideas about how to achieve this with simple inline code?

Thanks

3
  • 1
    if jQuery is an option try $('#review').toggle() Commented May 21, 2015 at 14:24
  • You are assigning 'none', need a double-equals in your ternary operator to test for 'none' Commented May 21, 2015 at 14:26
  • Not sure you get me, I'm saying this line: document.getElementById('review').style.display='none' ? 'block':'none' is ASSIGNING the value 'none' with the single-equals, which will always be true and set 'block', you need a double- (or triple-) equals there: document.getElementById('review').style.display=='none' ? 'block':'none' Commented May 21, 2015 at 14:59

2 Answers 2

1

Try this https://jsfiddle.net/muwyu286/. You have used an assign = instead of == in ('review').style.display='none' ? 'block':'none';"

<div onclick="document.getElementById('review').style.display = document.getElementById('review').style.display == 'none' ? 'block':'none';">
    <p>Click here to read our review</p>
    <div id="review" style="display: none;">Review text goes here.</div>
Sign up to request clarification or add additional context in comments.

Comments

0

You can look at this to learn about toggling through jquery.

Toggle Text with jQuery

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.