2

I am studying some source code of a website and would like to know how to trigger programmatically the ajax autocomplete on this text box. I tried to include just the relevant code.

html:

<div class="input-text xxl-width completer">
    <input type="text" placeholder="" id="textBoxText" name="textBoxText" class="predictiveText" autocomplete="off">

    <div class="autocompleter-wrapper" style="display: none;">
    </div>
</div>

javascript:

var w = this.$scope,
u = i.keyCodes,
v = this,
t = {
    $predictiveLookup: w,
    $predictiveInput: w.find("input.predictiveText"),
},

t.$predictiveInput.keydown(i.keyExecutor.onAllKeysExcept(function() {
    v.isKeyDown = true
}, [u.enter, u.keyUp, u.keyDown, u.escape]));

t.$predictiveInput.keyup(i.keyExecutor.onAllKeysExcept(function() {
    v.checkCompletion(n);
    v.isKeyDown = false
}, [u.enter, u.keyUp, u.keyDown, u.escape]));

When I call $(elem).keyup(); or $(elem).keydown(); Nothing happens. Is there any way to trigger these events programmatically?

3 Answers 3

4

Use trigger:

$(elem).trigger('click');

Execute all handlers and behaviors attached to the matched elements for the given event type.

Docs: http://api.jquery.com/trigger/

Sign up to request clarification or add additional context in comments.

Comments

1

It may be that $(elem).keyup() is indeed triggering your event correctly but that it's then falling foul of some logic somewhere inside keyExecutor.onAllKeysExcept which is comparing the pressed key against the supplied blacklist when no key is actually being pressed, meaning event.which is undefined.

You can simulate a specific key being pressed, perhaps one you would expect not to affect the input, using the following:

var e = $.Event('keyup');
e.which = 40; // (code for down arrow key say)
$(elem).trigger(e);

2 Comments

@stovrov I think you may be on to something. keyExecutor.onAllKeysExcept returns false for certain keycodes (13, 37, 38, 39, 40, 27, 9). I tried to trigger for any letter a-z but it still does not work. Any idea how i might call "v.checkCompletion(n);"
I've no idea what v.checkCompletion(n) is without seeing the full code I'm afraid, but setting e.which = 65 in the above code should have the same effect in your keyup handler as a genuine keyup event from the letter 'a' key. Of course you also have code handling keydown in your extract above so you have to consider what contribution this is making to the logic too.
0

keyup and keydown are original event handlers of the elements. You can't call them in some of the browsers directly after you used $() on them. You can use .trigger('eventtype')

or you can access the original element and call the original elements handlers on it like:

$(elem)[0].keyup()
$(elem)[0].keydown()

or to make sure its browser independent but still operating with the original element:

$(elem).toArray()[0].keyup()
$(elem).toArray()[0].keydown()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.