1

I use IE 11 with the scripting option enabled. Even on other browsers it does not work. Using notepad++ to code and run... I'm currently learning javascript. I have a .js and .html file - the html has 3 sets of headings/paragraphs where the paragraphs should only show if i click the headings. This does not work. I downloaded a copy of the java library as well... I assume it has something to do with the Doctype statement ? Any thoughts:

mcode.js

$(document).ready(function() {

  $("p").hide();

  $("h1").click(function() {
    $(this).next().slideToggle(300);
  });

});

myhtml.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>
 <title>Demo</title>
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>

<body>
 <h1>Heading one</h1>
 <p>This is just some text for heading 1</p>

 <h1>Heading two</h1>
 <p>This is just some text for heading 2</p>

 <h1>Heading three</h1>
 <p>This is just some text for heading 3</p>

<!-- FIRST BELOW POINTS TO WHERE THE JAVA SCRIPT LIBRARY IS   -->
<!-- SECOND IS MY JAVASCRIPT CODE THAT WILL BE USED           -->
<!--<script type="text/javascript" src="jquery-1.8.0.min.js"></script>-->
<script type="text/javascript" src="jquery-3.3.1.min.js"></script>
<script type="text/javascript" src="my_code.js"></script>
</body>

</html>
6
  • Check what the browser console has to say. Commented Nov 23, 2018 at 15:07
  • Your code is working fine. Do you have the jquery libary in the same directory as your html file? Do you have any console errors? Commented Nov 23, 2018 at 15:07
  • Works fine for me. I would suggest using an IDE to develop, makes things a lot easier than just using notepad. Commented Nov 23, 2018 at 15:08
  • Also I recommend to step away from xhtml. Html5 is far better and a well accepted standard these days. Commented Nov 23, 2018 at 15:09
  • Check the name of your JavaScript file, your comments say mcode.js but the HTML has an underscore. Also those two JavaScript files must be in the same location as you have it now. Commented Nov 23, 2018 at 15:12

2 Answers 2

2

You have listed your javascript as mcode.js in the question, but referenced src="my_code.js". Change your src in the html to the correct file and it should work fine.

<script type="text/javascript" src="mcode.js"></script>

That should be what you are after :)

Sign up to request clarification or add additional context in comments.

1 Comment

To increase your reputation, you might consider further filling out your profile.
1

You have to add mcode.js in your html file. Add a script in your head linked to mcode.js.

2 Comments

Oof these comments are aggressive. OP links a file called my_code.js but refers to js file as mcode.js in the question. Assuming his code works fine, then this answer, while it could use more elaboration, isn't unhelpful. :/
I'm not allowed to comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.