3
votes

Am using following script to show/hide div on button click

function showHide(divID){
if (document.getElementById(divID).style.display == "none") {
    $('#'+divID).fadeIn(3000);

} else {
    $('#'+divID).fadeOut(3000);
}
}

this is my HTML:

<button onClick="showHide('hideDiv',this.id)" type="button">English</button>
<button onClick="showHide('hideDiv',this.id)" type="button">Math</button>
<button onClick="showHide('hideDiv',this.id)" type="button">French</button>

and using a single div to display the content of the button on click

<div id="hideDiv" style="display:none;">
    <p>A painting workshop in the early Renaissance probably resembled</p>
</div>

When I click math after English, the content will hide, after one more click, the content is displayed again. But, I want to show the content when user click any button, I want to hide the "Hide" property here, so that user will get to see the content whichever button he clicks.

here is the fiddle link http://jsfiddle.net/ytyAd/

3
I don't understand. When should the div be hidden, and when should it be visible? - Aioros
create a jsFiddle with your issue, because your inline style of display:none, should hide your content by default when the page loads. But you're claiming that it's not. So go ahead and setup a jsFiddle with your issue. - klewis
the content should be visible, whichever button user clicks( English/french/math) - user1165077
So when do you want to hide this DIV? I cannot understand what you are trying to do. - A. Wolff
if i click math, content of the English should Hide, and content of the math should be shown in the same Div. That's what am looking for - user1165077

3 Answers

1
votes

Here's what I think you are looking for... http://jsfiddle.net/ytyAd/10/

JS put this in a $(document).ready():

$("button.showHide").click( function() {
    var content = $(this).text();
    $("#hideDiv > p").hide("slow");
    $("#hideDiv #"+content).show("slow");
});

HTML:

<button class="showHide" type="button">English</button>
<button class="showHide" type="button">Math</button>
<button class="showHide" type="button">French</button>

<div id="hideDiv">
    <p id="English" style="display:none;">english stuff</p>
    <p id="Math" style="display:none;">math stuff</p>
    <p id="French" style="display:none;">french stuff</p>
</div>

of course you can play around with it (different animations, callbacks, etc) to make it fit your context

0
votes

do you require something like this ?

Live Demo Here

HTML

<button id="engbutton" class="button" type="button">English</button>
<button id="mathbutton" class="button" type="button">Math</button>
<button id="frenchbutton" class="button" type="button">French</button>
<div id="engbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is English Div</p>
</div>
<div id="mathbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is Maths Div</p>
</div>
<div id="frenchbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is French Div</p>
</div>

CSS

.ContentDiv
{
    height:50px;
    width:200px;
    background:green;
}

jQuery

  $(".button").click(function(){
        $(".ContentDiv").hide();
        var divid = $(this).attr("id") + "Div";
        if($("#"+divid).css('display') == 'none')
        {
            $("#"+divid).fadeIn(3000);
        }
        else
        {
            $("#"+divid).fadeOut(3000);
        }  
    });
0
votes

HTML :

<div class="buttons">
<button id="engbutton" class="button" type="button">English</button>
<button id="mathbutton" class="button" type="button">Math</button>
<button id="frenchbutton" class="button" type="button">French</button>
</div>
<div class="contents">    
<div id="engbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is English Div</p>
</div>
<div id="mathbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is Maths Div</p>
</div>
<div id="frenchbuttonDiv" style="display:none;" class="ContentDiv">
    <p>This is French Div</p>
</div>
</div>

JS:

 $('.button').on('click', function(){
        $('.contents').find('div').hide();
        var getMessageDiv = '#'+$(this).attr('id')+"Div";
        if ($(getMessageDiv).is(':visible'))
        $(getMessageDiv).fadeOut();
         else 
        $(getMessageDiv).fadeIn();
    })

LIVE Demo