How i can get keydown event on iframe?
It's so late to answer but maybe is useful for somebody .
I've same problem,that means I've an iframe inside a HTML
page in a same domain
.
Now I need to get the Esc keyup event
.
however I don't want (or some times I can't) to write any script inside iframe body.
to do it, I use below script to detect what is the parent id of element that selected by clicked on it.
$(window).load(function () {
jQuery(jQuery('[id="frame"]')[0].contentWindow.document).on('keyup', function (e) {
var _currntItem = ($(e.target))
if (e.keyCode === 27) {
alert(_currntItem.parent().attr('id'));
}
});
This code resolved my problem.
hope to useful.
Adding event handler to an iframe using JQuery
$(document.getElementById('iframe_id').contentWindow.document).keydown(function() {
// my func
});
You need to access the document of the iframe which is through the ContentWindow object
$(document.getElementById('IFrameId').contentWindow.document).keydown(function(){ alert('Key down!'); });
Make sure you bind the event after the frame has loaded.