0

What am I doing wrong? I'm trying to get my jquery to show on my .html file, but it doesnt run!

<!DOCTYPE html>
<html>
<head>
<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
$(".mainNav a").hover(function() {
    $(this).parent().find(".subNav").slideDown('fast').show();
    $(this).parent().hover(function() {}, function() {
        $(this).parent().find(".subNav").slideUp('fast');
    });
}).hover(function() {
    $(this).addClass("subHover");
}, function() {
    $(this).removeClass("subHover");
});


</script>
9
  • where is the rest of the code? Commented May 1, 2017 at 4:23
  • Use document.ready for your jquery code. Currently jQuery might be loading later. Commented May 1, 2017 at 4:27
  • Where is HTML part? what you exactly trying to achieve? Wrap your code inside $(document).ready(function(){........//your code.....}); Commented May 1, 2017 at 4:27
  • I'll add the code shortly! Commented May 1, 2017 at 4:34
  • Do you get any error in console? Commented May 1, 2017 at 4:39

2 Answers 2

2

Make sure document is ready :

$(function() {
    //YOUR jQuery CODE GOES HERE
});

OR

Move your all scripts to the <body> of your html.

<body>

<script 
src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>

    $(".mainNav a").hover(function() {
        $(this).parent().find(".subNav").slideDown('fast').show();
        $(this).parent().hover(function() {}, function() {
            $(this).parent().find(".subNav").slideUp('fast');
        });
    }).hover(function() {
        $(this).addClass("subHover");
    }, function() {
        $(this).removeClass("subHover");
    });
</script>
</body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

Ok, adding it is fairly ugly. Adding $(document).ready(function(){........//your code.....}); isn't working. It's at codepen.io/SalvatoreSantamaria/pen/gWWaJm
Ugly?? And why is that? Your code is not participating in beauty contest, is it :) See the update for atlernative solution.
In the codepen link you provided dropdown is working fine, what is not working for you?
hey Lekhnath- the codepen works fine, I'm just trying to get it to work in an .html file. I still can't get it to work, even after adding the document ready or moving it to the body
0

2 way you have to fixed

1.add script in head and use

<script src="source of jquery"></script>
<script>
$document.ready(function(){

some code jQuery

})
</script>

2.add script before </body> tag

<script src="source of jquery"></script> 
<script>

enter code here here

</script>

this time you don't need $document.ready() anymore

1 Comment

in way two before tag </body>

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.