[javascript] jQuery show/hide not working

I have a basic html page with some JS and CSS:

_x000D_
_x000D_
$('.expand').click(function() {_x000D_
  $('.img_display_content').show();_x000D_
});
_x000D_
@charset "utf-8";_x000D_
_x000D_
/* CSS Document */_x000D_
_x000D_
.wrap {_x000D_
  margin-left: auto;_x000D_
  margin-right: auto;_x000D_
  width: 40%;_x000D_
}_x000D_
_x000D_
.img_display_header {_x000D_
  height: 20px;_x000D_
  background-color: #CCC;_x000D_
  display: block;_x000D_
  border: #333 solid 1px;_x000D_
  margin-bottom: 2px;_x000D_
}_x000D_
_x000D_
.expand {_x000D_
  float: right;_x000D_
  height: 100%;_x000D_
  padding-right: 5px;_x000D_
  cursor: pointer;_x000D_
}_x000D_
_x000D_
.img_display_content {_x000D_
  width: 100%;_x000D_
  height: 100px;_x000D_
  background-color: #0F3;_x000D_
  margin-top: -2px;_x000D_
  display: none;_x000D_
}
_x000D_
<html>_x000D_
_x000D_
<head>_x000D_
  <link href="beta.css" rel="stylesheet" type="text/css" />_x000D_
  <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>_x000D_
</head>_x000D_
_x000D_
<body>_x000D_
  <div class="wrap">_x000D_
    <div class="img_display_header">_x000D_
      <div class="expand">+</div>_x000D_
    </div>_x000D_
    <div class="img_display_content"></div>_x000D_
  </div>_x000D_
  </div>_x000D_
</body>_x000D_
_x000D_
</html>
_x000D_
_x000D_
_x000D_

A click on the div with the class expand does nothing... I also have tried to copy/paste an example code from jQuery website but it also didn't work. Can anyone help me with this problem?

This question is related to javascript jquery html css

The answer is


Just add the document ready function, this way it waits until the DOM has been loaded, also by using the :visible pseudo you can write a simple show and hide function.

Sample

 $(document).ready(function(){

    $( '.expand' ).click(function() {
         if($( '.img_display_content' ).is(":visible")){
              $( '.img_display_content' ).hide();
         } else{
              $( '.img_display_content' ).show();
         }

    });
});

Demo

_x000D_
_x000D_
$( '.expand' ).click(function() {_x000D_
  $( '.img_display_content' ).toggle();_x000D_
});
_x000D_
.wrap {_x000D_
    margin-left:auto;_x000D_
    margin-right:auto;_x000D_
    width:40%;_x000D_
}_x000D_
_x000D_
.img_display_header {_x000D_
    height:20px;_x000D_
    background-color:#CCC;_x000D_
    display:block;_x000D_
    border:#333 solid 1px;_x000D_
    margin-bottom: 2px;_x000D_
}_x000D_
_x000D_
.expand {_x000D_
 float:right;_x000D_
 height: 100%;_x000D_
 padding-right:5px;_x000D_
 cursor:pointer;_x000D_
}_x000D_
_x000D_
.img_display_content {_x000D_
    width: 100%;_x000D_
    height:100px;   _x000D_
    background-color:#0F3;_x000D_
    margin-top: -2px;_x000D_
    display:none;_x000D_
}
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div class="wrap">_x000D_
<div class="img_display_header">_x000D_
<div class="expand">+</div>_x000D_
</div>_x000D_
<div class="img_display_content"></div>_x000D_
</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

http://api.jquery.com/toggle/
"Display or hide the matched elements."

This is much shorter in code than using show() and hide() methods.


if (grid.selectedKeyNames().length > 0) {
            $('#btnRemoveFromList').show();
        } else {
            $('#btnRemoveFromList').hide();
        }

}

() - calls the method

no parentheses - returns the property


Use this

<script>
$(document).ready(function(){
    $( '.expand' ).click(function() {
        $( '.img_display_content' ).show();
    });
});
</script>

Event assigning always after Document Object Model loaded


The content is not ready yet, you can move your js to the end of the file or do

<script>
$(function () { 
    $( '.expand' ).click(function() {
       $( '.img_display_content' ).show();
    });
});

So that the document waits to be loaded before running.


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width