简体   繁体   中英

is it possible to hide div when another button is click?

I am trying to hide the div 's when different buttons are clicked but I don't know how to. (So when 'Test 1' is clicked it should hide 'Test 2' Div and vice versa) I checked here and on Google but couldn't find an answer for it.

Javascript :

function showHide(divId) {
    var theDiv = document.getElementById(divId);
    if (theDiv.style.display == "none") {
        theDiv.style.display = "";
    } else {
        theDiv.style.display = "none";
    }
}

HTML :

<input type="button" onclick="showHide('hidethis')" value="Test It"> 
<div id="hidethis" style="display:none">
<h1>TEST ME!</h1>>
</div>

<input type="button" onclick="showHide('hidethis2')" value="Test It 2"> 
<div id="hidethis2" style="display:none">
<h1>TEST MEEEEEEEEEEEEEEE 2!</h1>
</div>

JSFIDDLE: is not doing it here but works locally

http://jsfiddle.net/S5JzK/

<input type="button" onclick="showHide('hidethis')" value="Test It" /> 
<div id="hidethis" style="display:none">
<h1>TEST ME!</h1>
</div>

<input type="button" onclick="showHide('hidethis2')" value="Test It 2"> 
<div id="hidethis2" style="display:none">
<h1>TEST MEEEEEEEEEEEEEEE 2!</h1>
</div>


function showHide(divId) {
    $("#"+divId).toggle();
}

Check the Fiddle http://jsfiddle.net/S5JzK/7/

  1. To it work in fiddle, in your example, you need to select (No wrap - in head) on the left.

  2. Look the example below, using pure javascript:

HTML

<input type="button" onclick="showHide('hidethis')" value="Test It"> 
<div id="hidethis" style="display:none">
    <h1>TEST ME!</h1>
</div>

<input type="button" onclick="showHide('hidethis2')" value="Test It 2"> 
<div id="hidethis2" style="display:none">
    <h1>TEST MEEEEEEEEEEEEEEE 2!</h1>
</div>

JAVASCRIPT

function showHide(divId) {

    /* Hide all divs */
    var elements = document.getElementsByTagName('div');

    for (var i = 0; i < elements.length; i++) {
        elements[i].style.display = "none";
    }

    /* Set display */
    var theDiv = document.getElementById(divId);
    theDiv.style.display = "";
}

http://jsfiddle.net/S5JzK/9/

ANOTHER JAVASCRIPT EXAMPLE

function showHide(divId) {

    /* Hide the divs that you want */
    var div1 = document.getElementById('#hidethis');
    var div2 = document.getElementById('#hidethis2');

    div1.style.display = "none";
    div2.style.display = "none";

    /* Set display */
    var theDiv = document.getElementById(divId);
    theDiv.style.display = "";
}

Please try this, it works well and so simple,

<html>
<head>
<style>
.manageDiv{
    display:none;

}
</style>
    </head>
    <body>
<input type="button" class="testButton" value="Test It" /> 
<input type="button" class="testButton" value="Test It 2" /> 

<div id="hidethis2" class="manageDiv">
<h1>TEST MEEEEEEEEEEEEEEE 2!</h1>
</div>
</body>
</html>

$(function(){    
    $(".testButton").on("click", function(){
                        $("#hidethis2").toggleClass("manageDiv");

                        });
});

Using JQuery:

function showHideDiv(divId, bShow) {
  if (bShow) {
    $("#" + divId).show();
  } else {
    $("#" + divId).hide();
  }
}

your code seems fine. are you sure you enter the function upon click? try adding a breakpoint using developer tools or an alert.

Anyways, I see you tagged this post with jquery. you can you it to do the task more elegantly.

   $("#" + theDiv).hide();

or for showing it:

   $("#" + theDiv).show();

"JSFIDDLE: is not doing it here but works locally"

Yes, because by default jsfiddle wraps your JS in an onload handler, which means the function declaration is local to that handler. Inline html attribute event handlers like your onclick="showHide('hidethis')" can only call global functions.

Under jsfiddle's Frameworks & Extensions heading there's a drop-down where you can change the default "onload" to "No wrap - in head" (or "No wrap - in body"). That'll make your function declaration global as in your local implementation.

Demo: http://jsfiddle.net/S5JzK/8/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM