0

I have an image upon which I want to bring a text on clicking the image. I applied the css display property to none and on click I changed it to block. Now again on clicking I want to change the display to none. How can I do that?

js:-

function showTerms(data){
    document.getElementById(data).style.display = 'block';
}

html:-

<div style="display: none;" id="text1111" class="offer_text size-12">
    <div class="TC">
        <div class="condition">Terms &amp; Condition</div>
    </div>
    <p></p>
    <div><br></div>
    <div>- The coupon code is valid for one time use per user account.<br></div>
    <p></p>

2 Answers 2

1

You can achieve this with a toggle function, like so:

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

10 Comments

Its not changing again to none
Actually on click the text is on the image. In your demo its above the image so I guess its not able to get the id of image
try to change the z-index.
@KshitijPandey, provide your complete HTML/CSS... Why do you need image id at all? However, code in answer must work, for sure....
You will have to post your html css and js, you have a class "offer_text size-12." Without that no one might be able to help.
|
0

Generally speaking, do not modify CSS directly in JavaScript (there are of course exceptions, but in this case you shouldn't).

Instead, define a CSS class like so:

.shown {display: block}

Then your JavaScript can be as simple as:

function showTerms(data) {
    document.getElementById(data).classList.toggle("shown");
}

* If support for old browsers is needed, you'll need something a little more advanced to check if the class is on and toggle it manually.

2 Comments

@KshitijPandey Ah, remove the inline display:none and put that in CSS too, otherwise it continues to override.
If I am changing the inline css the text is already appearing on the image which I want to do it by clicking the image

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.