Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the visibility of a <div> tag using javascript in the same page?

I'm working on a simple javascript code.

Based on the condition I should change the visibility of the tag from hidden to visible.

I should use javascript alone. No jQuery or AJAX. Any idea or suggestion please.

like image 744
arunpandiyarajhen Avatar asked Jan 16 '13 10:01

arunpandiyarajhen


People also ask

How do you make a division visible in JavaScript?

Another way to make a div visible or invisible is with the display property. Then we can make it visible with: const div = document. querySelector('div') div.

How do you make a div invisible in JavaScript?

The document. getElementById will select the div with given id. The style. display = "none" will make it disappear when clicked on div.

How do you make a div invisible and visible in HTML?

The hidden attribute hides the <div> element. You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid. A hidden <div> element is not visible, but it maintains its position on the page.


2 Answers

Using the visibility attribute:

Show the div with id="yourID":

document.getElementById("yourID").style.visibility = "visible";

To hide it:

document.getElementById("main").style.visibility = "hidden";

Using the display attribute:

Show:

document.getElementById("yourID").style.display= "block";

Hide:

document.getElementById("yourID").style.display= "none";
like image 196
mornaner Avatar answered Nov 02 '22 16:11

mornaner


<div id="contentDiv">
    This is the content .
</div>

if you want to change the visibility of div with id="contentDiv" using javascript then you can do with..

var eleDiv = document.getElementById("contentDiv");


    // based on condition you can change visibility
if(eleDiv.style.display == "block") {
        eleDiv.style.display = "none";
}
else {
    eleDiv .style.display = "block";
}

Hope this code helps you........

like image 32
Hitesh Bavaliya Avatar answered Nov 02 '22 16:11

Hitesh Bavaliya