0

There are a lot of posts on this site with the same title, but none of them helped with my problem. I'm trying to do a simple thing with jQuery, and it's not doing anything, which leads me to believe that jQuery isn't even properly linked. But I'm doing exactly what I've always done. It's supposed to change the unordered list in this div to change its background color to orange. It doesn't change at all, though. I even tried to use a console.log to see if even that would work. It didn't. Can someone check this out?


My code :

<script src="jquery-1.12.0.js">
    $(document).ready(function() {
        console.log("Why isn't this working?")
        $("#contentleft ul").css("background-color", "#FFA500");
    });
</script>

<div id="contentleft">
    <h3>My List Is Awesome</h3>
    <ul>
        <li><a href="http://www.fullsail.com">Full Sail</a></li>
        <li><a href="http://www.cnn.com">CNN</a></li>
        <li><a href="http://www.youtube.com">YouTube</a></li>
        <li>No link here.</li>
    </ul>
</div>
elixenide
  • 44,308
  • 16
  • 74
  • 100
Matt Lee
  • 521
  • 2
  • 5
  • 16

2 Answers2

6

You can't put code inside <script> tag that has an src.

Use two script tags

<script src="jquery-1.12.0.js"></script>

<script>
    $(document).ready(function() {
        console.log("Why isn't this working?")
        $("#contentleft ul").css("background-color", "#FFA500");
    });
</script>
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • What the heck? It worked this time. I actually tried that and it didn't work before! Maybe that's when I was using .contentleft without the hash mark. I was treating it like a class by mistake at first. Thanks! I really appreciate it! – Matt Lee Feb 04 '16 at 23:00
5

Don't try to use one <script> tag with both a src attribute and internal contents. Break it into two tags, like this:

<script src="jquery-1.12.0.js"></script>
<script>
    $(document).ready(function() {
        console.log("Why isn't this working?")
        $("#contentleft ul").css("background-color", "#FFA500");
    });
</script>

A <script> with both a src and internal contents will give unpredictable results. You might want to read this post for more information.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • What the heck? It worked this time. I actually tried that and it didn't work before! Maybe that's when I was using .contentleft without the hash mark. I was treating it like a class by mistake at first. Thanks! I really appreciate it! – Matt Lee Feb 04 '16 at 22:59