Search code examples
jquerycsshtmltumblr

How to show a div when another div is clicked?


The question is pretty simple really and I know there must be an obvious answer out there. I have a div id called #pagebox and I want it to display when #about is clicked on.

Thank you; I would include HTML and CSS but I don't believe it would be of much help.


Solution

  • Working jsFiddle here

    This is really all you need:

    $('#about').click(function() {
        $('#pagebox').show();
    });
    

    Notes:

    You should put your code inside a document ready function, like this:

    $(document).ready(function(){
        //above code goes here
    });
    

    This solution uses jQuery, so you must include a reference to the jQuery library in the document head tags, like this:

    <head>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    </head>
    

    If the code does not exist in the DOM originally but is injected, either via javascript/jQuery or via AJAX, then you must should use this format:

    $('#about').on('click', function(){
        $('#pagebox').show();
    });
    

    Sources/References:

    Alex Garret's 200 10-min beginner videos about jQuery (FREE)
    W3School's excellent jQuery selector reference chart
    More from Alex Garret (also free)