Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript/jQuery newbie mistake with mouseenter event causing syntax error

I am trying to apply a style when the mouseenter event is triggered, but if I uncomment the following untouched selector - even the document ready stops working.

<body>
<div id="1" class="button untouched"></div>
<script src="/jquery.js"></script>
<script>
$(document).ready(function(){
    alert("JQuery is working");
});

/*
$(".untouched").mouseenter($function(){
    $(this).addClass("touched");
});
*/
</script>
</body>

I am following the example found at:

http://api.jquery.com/mouseenter/

And I get the following error in Firebug:

missing ) after argument list
[Break On This Error]   

$(".untouched").mouseenter($function(){

Since it does not work, I've made a mistake, but I don't know what. All I know is that none of my code works if I let that run. I downloaded the latest 1.7.2 version of jQuery, which I know is available on the page because the alert() runs with the other commented out.

like image 931
Adrian Cornish Avatar asked Jan 16 '23 11:01

Adrian Cornish


2 Answers

No need for the $ in front of the function. Also, the mouseenter event function code should be inside the document ready.

<script>
     $(document).ready(function(){
         alert("JQuery is working");

         $(".untouched").mouseenter(function(){
             $(this).addClass("touched");
         });
     });
</script>
like image 165
Bart Wegrzyn Avatar answered Jan 31 '23 07:01

Bart Wegrzyn


In your script the $(".untouched") part should be within the ready function. Also,

mouseenter($function(){ the $ sign is not correct.

Your final script should look like this:

$(document).ready(function(){
    alert("JQuery is working");

    $(".untouched").mouseenter(function(){
        $(this).addClass("touched");
    });
});
like image 23
xbonez Avatar answered Jan 31 '23 09:01

xbonez