Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display a div triggered by onclick event

I have have two divs. I want to display a div( which has other divs inside it ) when the onclick event is triggered.

Any help or suggestion would be appreciated.

Mockup of a div containing a submit button and another div

like image 244
NullPoiиteя Avatar asked Nov 28 '22 14:11

NullPoiиteя


2 Answers

Here you go:

div{
    display: none;
}

document.querySelector("button").addEventListener("click", function(){
    document.querySelector("div").style.display = "block";
});

<div>blah blah blah</div>
<button>Show</button>

LIVE DEMO: http://jsfiddle.net/DerekL/p78Qq/

like image 157
Derek 朕會功夫 Avatar answered Nov 30 '22 04:11

Derek 朕會功夫


You'll have to give an ID to the div you want to show/hide, then use this code:

html:

<div id="one">
    <div id="tow">
        This is text
    </div>
    <button onclick="javascript:showDiv();">Click to show div</button>
</div>

javascript:

function showDiv() {
    div = document.getElementById('tow');
    div.style.display = "block";
}

CSS:

​#tow { display: none; }​

Fiddle: http://jsfiddle.net/xkdNa/

like image 20
Vishal Avatar answered Nov 30 '22 04:11

Vishal