1

I have an HTML page with some paragraphs which I would like to show or hide via Javascript. What should I do? Do I have to use the display property in the CSS file too? Thank you

EDIT: Since the paragraphs will be the error messages of a form, I'd like that at the beginning none of them were visibile.

2
  • it depends on when you want to hide them ... Commented Dec 7, 2013 at 11:55
  • provide some code what you tried so far Commented Dec 7, 2013 at 11:55

2 Answers 2

8

You can do like this :

To hide :

document.getElementById("elementId").style.display = 'none';

To show:

document.getElementById("elementId").style.display = 'block';
Sign up to request clarification or add additional context in comments.

2 Comments

First set their css as display:none; and then show it using js as above.
I would recommend (if possible) not to show elements by setting node.style.display = 'block';. I would rather just set node.style.display = ''; so display: none gets removed and there is no need to override display property (as it could be different from "block"). Of course this wouldn't work if there is some display: none refering to that node in CSS stylesheet.
-2

To hide initially do this and better you put them in span not para

 $(document).ready( function() {
     $("span").hide();
 });

And whenever you need them to show call a javascript function on submit button and show using same

$("span").show();

But do not do like this this will show all messages, Put your if else logic and than show them by Id or class using jquery

$("#id").show();
$(".class").show();

1 Comment

OP has not requested anything to do with jQuery. This question does also not have a jQuery tag attached so a jQuery solution is not helpful in the context of providing a JavaScript only solution.

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.