I'm trying to trigger the propertychange event on an input field for a bookmarklet (the field is OpenGrok's search field).
The handler for that field is defined like this (this is the de-minified OpenGrok source):
function(i) {
var h = true;
if (i.type == "propertychange") {
h = i.originalEvent.propertyName.toLowerCase() == "value"
}
if (h) {
if (d(this).data("timeout")) {
clearTimeout(d(this).data("timeout"))
}
d(this).data("timeout", setTimeout(function() {
f._applySearchTermFilter()
}, f.config.searchTimeout))
}
}
I tried (yes, the text field has neither name nor id, just a placeholder attribute...)
$('input[placeholder="Click here to select project(s)"]')
.focus()
.val('MY-SEARCH-TEXT')
.trigger("propertychange")
but that fails because the handler tries to access event.originalEvent, which jQuery doesn't set. Can I somehow fake an event which has this property?
Maybe it would alternatively be possible to go without an event and call _applySearchTermFilter() directly, but I also don't know how to do that, since all these functions are inside an anonymous IIFE (and f=this). Is such a function exposed through window in any way?
Found it, it's actually quite simple when one knows jQuery better:
$('input[placeholder="Click here to select project(s)"]')
.focus()
.val('MY-SEARCH-TEXT')
.trigger($.Event('propertychange', {originalEvent: {propertyName: "value"}}));