I am trying to pass keyboard events to a special class of HTML element (mathquill span). (Key presses get caught at a higher level, so I have to spoon-feed them down.) The standard way of doing this seems to be with jQuery's .trigger, as proposed in this previous question. However, that solution does absolutely nothing on my end. I cannot for the life of me find what's wrong.
This code is supposed to create a MathQuill box and then enter the letter 'f' into it. But the box remains empty.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mathquill-master/mathquill.css">
</head>
<body>
<!-- loading external scripts -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="mathquill-master/mathquill.min.js"></script>
<!-- MathQuill box -->
<span class="mathquill-editable" id="mathquill_box"></span>
<script type="text/javascript">
$(function() {
$('.mathquill-editable').focus();
// creating 'fake' custom event (pressing the 'F' key)
// (this is taken directly from the answer to the previous question)
var customKeyDownEvent = $.Event('keydown');
customKeyDownEvent.bubbles = true;
customKeyDownEvent.cancelable = true;
customKeyDownEvent.charCode = 70; // key code for 'F'
customKeyDownEvent.keyCode = 70;
customKeyDownEvent.which = 70;
// send the event to the MathQuill box
$('.mathquill-editable textarea').trigger(customKeyDownEvent);
});
</script>
</body>
</html>
Any help would be immensely appreciated!