How to add javascript to HTML Page?

DSM

I am new to web development and I want to add following function to my simple html page but if I press "click me" does not happen anything. description part does not hide and show

This is the code I tried

I added those codes as below. CSS is working perfectly. but JavaScript does not work. How can I fix this issue

<html>
<head>
<title>TEST</title>
<link rel="stylesheet" type="text/css" href="hpcss.css">
<script>
// Hide all the elements in the DOM that have a class of "box"
$('.box').hide();

// Make sure all the elements with a class of "clickme" are visible and bound
// with a click event to toggle the "box" state
$('.clickme').each(function() {
    $(this).show(0).on('click', function(e) {
        // This is only needed if your using an anchor to target the "box" elements
        e.preventDefault();

        // Find the next "box" element in the DOM
        $(this).next('.box').slideToggle('fast');
    });
});
</script>


</head>
<body align="center">
<a href="#" class="clickme">Click Me</a>
<div class="box">
    First Description
</div>

<a href="#" class="clickme">Click Me</a>
<div class="box">
    Second Description
</div>


</body>

</html>
Hugevn

you should do somethings like that:

$(document).ready(function() {
  // add your code here
})

you run javascript before finishing load html --> error

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to add button using javascript in html page dynamically

From Dev

how to add read more in a page by using HTML,javascript

From Dev

How can i add a javascript file in html page?

From Dev

How to add video to html page ?

From Dev

How to add javascript to HTML?

From Dev

Add an attribute to html element with javascript on page load?

From Dev

Add HTML stuff to page using JavaScript

From Dev

How to add html styling in html page in python

From Dev

How to add html styling in html page in python

From Dev

How to use this javascript on HTML page?

From Dev

How to use this javascript on HTML page?

From Dev

How to load a html page in javascript

From Dev

How to add html page to another html page by using object tag?

From Dev

Using javascript, how to add $ sign before any input element on an HTML page

From Dev

how to add(link) my customized javascript code to any html/php page

From Dev

How to add HTML headers and footers to a page?

From Dev

How to add a picture in html page in Spring application

From Dev

How to add blank HTML page to Magento?

From Dev

How to add jQuery code into HTML Page

From Dev

How to add a bottom toolbar in an html page

From Dev

How to add a picture in html page in Spring application

From Dev

How to add a bottom toolbar in an html page

From Dev

How to add div field dynamically in to a html page?

From Dev

How can I add an html page into wordpress?

From Dev

How to add HTML page to React application

From Dev

How to add background video to html page?

From Dev

How to add javascript in page_load

From Dev

How to add JavaScript just for one specific page?

From Dev

How to properly link Javascript to html page

Related Related

HotTag

Archive