views:

55

answers:

3

Hi, I am currently trying to use jquery to enter a selection from a dropdown into a text box, simulate a click to select it, press return, wait for some processing and then press return again. It's quite a nasty way of getting what I need but it's the only way I can see at the moment. Here is the code:

$('#fav').change(function() 
{
  $('#contract_input').val($('#fav').val());
  $('#contract_input').trigger('click');
  e = jQuery.Event("keypress");
  e.which = 13;
  $('#contract_input').trigger(e).delay(500).trigger(e);
}

The issue I am having is that IE8 is giving an error on the page:

'Event' is not defined

The click seems to work, it's just the return that does not.

Any ideas?

Many thanks
Cap

A: 

It really sounds odd and wrong, anyway I would try it like this:

$('#contract_input').trigger({
   type:  'keypress',
   which: 13
});

Example: http://www.jsfiddle.net/YjC6y/3/

You should take into consideration to call the underlaying event handler directly instead of this.

.triggerHandler() and .trigger() should do it right

jAndy
Oh I totally agree :) It's one of those taking over someone else's work with a tiny deadline jobs and so it's messy as anything and as I say I'm no javascript expert..Anyway no joy I'm afraid. The odd thing is that the first key press, not the second, works when running in Visual Web Developer 2008 but only the click works when running on the server. Same code in both.
Captastic
A: 

Edit again: $('#contract_input').keypress(function (e) { do Stuff }); Should be used instead http://api.jquery.com/keypress/

Meke
What he has is correct: http://api.jquery.com/category/events/event-object/
Nick Craver
This is totally wrong. What you are doing is appending a keypress-event-handler to the input-field not triggering a keypress-event.
jitter
Been reading the question a few times now and.. You're right.. I think.
Meke
+1  A: 

Consider using keydown instead of keypress and in IE you could try setting e.keyCode instead of e.which.

e.g.

e = jQuery.Event("keydown");
e.keyCode = 13;
$('#contract_input').trigger(e).delay(500).trigger(e);
jitter
no joy sadly, same errors, thanks anyway :)
Captastic