0

This is my first attempt to use jquery, javascript and HTML/CSS in one site. I have a working inline script tag that utilizes jquery and javascript. Unfortunately I can't seem to create ANY structure on the page (an h1 title, an aside, moving the rendered javascript anywhere...) while this script is in my index.html. I've done lots of research but there is some fundamental I'm missing here as I can have only one or the other. Can anyone point me in the right direction? Here's a curtailed section:

<body>
<script>
  $(document).ready(function(){
    var $body = $('body');
    $body.html('');
    //code code code
    }
  });
</script>

I've tried putting my html within the body and outside of the script, in the script but outside of the jquery function, and inside the jquery function. How could I get a simple "Hello World" up on this page?

2
  • The problem is that you put two closing curly brackets for just one function tag. }); is enough. Remove the "lonely" curly brackets Commented Apr 7, 2015 at 19:14
  • What is //code code code? This : $body.html('') clears all the content of your page... Commented Apr 7, 2015 at 19:14

2 Answers 2

2

What's happening is that once the page is finished loading, or ready, you are setting the HTML to nothing via $body.html('').

You could try changing it to $body.append('This was generated by jQuery!') and you'll see your hardcoded HTML in addition to some text.

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

3 Comments

I think the presented Javascript is not fully accurate of what is being used, as it must be running if the body is coming up empty when HTML is present on the page.
Thanks! That was the issue. I'm still pretty new to jquery and javascript, so silly things like this can derail me for entirely too long.
I feel you, I remember going through similar experiences! One thing that helps a lot during learning javascript testing is console.log(), which outputs text or values on to your browser's Javascript console. Good luck!
1

Your Script is a Child of the Body, which inner html you are clearing by asigning an empty String - deleting your inline Script in the process. Have you tried moving your Script to the Head section?

Comments

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.