Aminadav Glickshtein
Aminadav Glickshtein

Reputation: 24600

How to check if mouseenter an iframe in pure javascript?

I am sure that it is possible, because I was able to do it using jQuery.

$('iframe').mouseenter(function(){
    alert('jquery-work')
});

document.getElementById('iframe').addEventListener('mouseenter',function(){
    alert('vanilla-js not prompt')
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<iframe id=iframe src=http://www.bing.com></iframe>
Try to enter and leave the ifame<br>
I want the vanilla js to execute, but only js function execute

Upvotes: 0

Views: 443

Answers (2)

Leeish
Leeish

Reputation: 5213

Just looking at the above code, your jQuery call is not looking for an element with the ID of iframe but any iFrame element. If you add id="iframe" to your iframe, your vaniall code should work.

The equivilant to your bottom code in jQuery is actually

$('#iframe') //element with the id of iframe

$('iframe') //Any iFrame elements

$('.iframe') //element with the class of iframe

Here is answer:

document.getElementById('iframe').addEventListener('mouseenter',function(){
alert('vanilla-js not prompt')
})

http://jsfiddle.net/0uz19cmu/4/

Upvotes: 2

jwatts1980
jwatts1980

Reputation: 7356

Is this what you want? http://jsfiddle.net/0uz19cmu/3/

document.getElementsByTagName('iframe')[0].onmouseenter = function() {
    alert('vanilla-js not prompt');
};

This will only get the first iframe in the document. If you want to get a specific one, then I would recommend using an id.

Upvotes: 2

Related Questions