Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
49
rated 0 times [  53] [ 4]  / answers: 1 / hits: 31216  / 10 Years ago, fri, june 27, 2014, 12:00:00

I am adding a listener like so:



    window.addEventListener('native.showkeyboard', function (e) {
...
...
});


I'm writing a unit test for this so I want to trigger the event. Im doing:



    window.trigger('native.showkeyboard');


But I end up with an error for that line saying:



    undefined is not a function


How can I manually trigger this event?



EDIT



I have also tried:



  $(window).trigger('native.showkeyboard');


but the handler doesnt run with this as it's not registered with jquery...


More From » jquery

 Answers
72

If you are triggering the event via jQuery then the event ought to have been attached via jQuery -- see @fredericHamidi's comment.



$(window).on('native.showkeyboard', function (e) {
.........
});

$(window).trigger('native.showkeyboard');


WORKING JSFIDDLE DEMO



Or if you're using plain vanilla JS do it this way:



window.addEventListener('native.showkeyboard', function (e) {
........
});

window.dispatchEvent( new Event('native.showkeyboard') );


WORKING JSFIDDLE DEMO


[#70398] Thursday, June 26, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
grayson

Total Points: 36
Total Questions: 113
Total Answers: 95

Location: Tonga
Member since Fri, Aug 21, 2020
4 Years ago
grayson questions
;