0

I have started learning Jquery today from W3Schools. I am trying some examples.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            $('.mouseover').mouseover(function () {
                alert("Entered")
            })
        })
    </script>
<p class="mouseover">Enter and leave from here</p>
<p class="click">Click and double click here</p>
<p class="hover">Hover over here</p>

I want to assign two functions to the same class mouseover which takes two events mouseenter and mouseleave. I do not want to use hover. I just want to know the process how it can be done?

To assign different methods by selecting a same element. Like clicking on a class will do one thing, hovering over it will do other and so on.

1
  • You can write mouseleave() event. Use event preventDefault() or maybe event stopPropagation() Commented Aug 21, 2019 at 10:43

3 Answers 3

1

No need to write separate functions. You can chain both on one object like this

$('.mouseover').on('mouseover', function() {
  console.log("Entered");
}).on('mouseleave', function() {
  console.log("Leave");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="mouseover">Enter and leave from here</p>
<p class="click">Click and double click here</p>
<p class="hover">Hover over here</p>

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

Comments

0

You should simply assign another event to your selector. Smth like this:

let $el = $('.mouseover');    

$el.on('mouseover', function() {
  console.log("Entered");
});

$el.on('mouseleave', function() {
  console.log("Leave");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="mouseover">Enter and leave from here</p>
<p class="click">Click and double click here</p>
<p class="hover">Hover over here</p>

2 Comments

my IDE says there are duplicate select statements
I've updated my answer. It was only example, your IDE should give you a hot fix for this simply case.
0

Try the following, the problem you can have is that for clicking the item "mouseover" you have to hover it so it triggers the hovering function first... :

    $(document).ready(function () {
        $('.mouseover').mouseover(function () {
            alert("Entered")
        })
    });

        $('.mouseover').click(function () {
            alert("Clicked!")
        });
        
        $('.click').click(function () {
            alert("Clicked!")
        });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="mouseover">Enter and leave from here</p>
<p class="click">Click and double click here</p>
<p class="hover">Hover over here</p>

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.