Lemex
Lemex

Reputation: 3794

jQuery Auto-complete - Force Selection/Testing

I have a simpe jQuery autocomplete input box with the ID 'medID'

$('#medID').val("Par").focus().keydown();

I use the code above to put a value in the box, trigger focus and then key down to search.

It returns between 1-5 values.

How do i get it automaticly select one of the suggested values?

I have tried:

$('#medID').children()[0].focus().click();

and

$('#medID').children().focus().click();

But the suggested auto complete values are direct chilren of the input box?

This is so i can test this part of my system without the need of a user.

Upvotes: 0

Views: 307

Answers (2)

Rune Vejen Petersen
Rune Vejen Petersen

Reputation: 3239

The jQuery UI team do something similar for their own unit tests for autocomplete. See this answer for more details.

Upvotes: 0

Dan Fox
Dan Fox

Reputation: 1723

I'm not 100% certain, but I think browsers prohibit this. Your request might turn out to be impossible.

If it were possible, a malicious script could gain access to data that users had previously typed into input fields, e.g. credit card numbers or email addresses, using a script just like the one you outlined. This would be a pretty bad security hole.

Sorry about that. Perhaps you could get away with just looping through the input values your want to test and calling $('#medID').val(testValueHere) instead?

Upvotes: 1

Related Questions