1

I thought I would share this snippet that I use to show and hide divs. Does anyone have any improvements?

function toggle(obj) {
    var el = document.getElementById(obj);
    if ( el.style.display != 'none' ) {
    	el.style.display = 'none';
    }
    else {
    	el.style.display = 'block';
    }
}
flag

1 Answer

2

If you're attaching this to a button action then you could try some code like this:

$("button").click(function () {
    $("p").toggle();
});

You just need to have the jquery include, which you can pull right from google

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

and a $(function() { }); around it :)

Read about jquery here: http://jquery.com/

and the toggle function here: http://docs.jquery.com/Effects/toggle

link|flag

Your Answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.