jQuery — Height, Hide, ID, and More

Image for post
Image for post
Photo by jonathan romain on Unsplash

jQuery is a popular JavaScript for creating dynamic web pages.

In this article, we’ll look at how to using jQuery in our web apps.


The .height() method gets the current computed height for the first element in the set of matched elements.

It can also ser the height of every matched element.

For example, we can get the height of window by writing:


And if we have a div:


with the following CSS:

div {
width: 50px;
height: 70px;
float: left;
margin: 5px;
background: red;
cursor: pointer;

We can set its height when we click it by writing:

$("div").one("click", function() {
cursor: "auto",
backgroundColor: "green"

The height is in pixels.

:hidden Selector

We can use the :hidden select to select all elements that are hidden.

For example, if we have:

<div style="display:none;">Hidden</div>
<div class="starthidden">Hidden</div>
<input type="hidden">
<input type="hidden">
<input type="hidden">

and the following CSS:

.starthidden {
display: none;

Then we can get all the hidden elements that aren’t script elements buy writing:

const hiddenElements = $("body").find(":hidden").not("script");

We can also show the hidden divs by writing:



The .hide() method hides the matched elements.

For example, if we have:

<div id="clickme">
Click here
<img id="book" src="https://i.picsum.photos/id/23/200/200.jpg?hmac=IMR2f77CBqpauCb5W6kGzhwbKatX_r9IvgWj6n7FQ7c" alt="">

Then we can hide the image when we click Click here by writing:

$("#clickme").click(function() {
$("#book").hide("slow", function() {
alert("Animation complete.");

The first argument of hide is the animation tup;e.

And the 2nd argument is the callback that’s called when the animation is done.


The hover method lets us bind event handlers for hovering over and hover out of the element.

For example, if we have:

<li class="fade">Chips</li>
<li class="fade">Socks</li>

Then we can add *** to the li text when we hover over it by writing:

function() {
$(this).append($("<span> ***</span>"));
function() {
$("li.fade").hover(function() {

The *** will disappear when we move our mouse outside.


The .html() method gets the HTML content of the first element in the set of matched elements.

It can also be used to set the HTML of every matched element.

For example, if we have:

<b>Click</b> to change the <span id="tag">html</span>

Then we can click on the p element to change the text to its raw HTML form by writing:

$("p").click(function() {
const htmlString = $(this).html();

We call html in the first line to get the HTML code, then we set that as the text value.

It’ll be escaped when we set it as the text content so that we’ll see the raw code displayed.

ID Selector (“#id”)

We can select a single element with the ID selector.

For example, if we have:

<div id="myDiv">id="myDiv"</div>

Then we can add a red border to the div by writing:

$("#myDiv").css("border", "3px solid red");


We can set the height, hide elements, get its HTML code, and more with jQuery.

Written by

Web developer. Subscribe to my email list now at http://jauyeung.net/subscribe/. Email me at hohanga@gmail.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store