views:

234

answers:

2

I have a form with 2 text fields which get populated using auto complete. Now when I enter some value in form 1 (through autocomplete ), I want the second form field to fetch the auto complete values using text entered in field1 as one of the parameters. Lets say ,

text 1 < contains car brand names which are auto populated >

text 2 < models of all cars again auto populated > ( key = car brand )

After entering the value in text 1 e.g Ford , I want to trigger an event which will load the auto fill in text2 taking 'Ford' as key i.e load all cars belonging to 'Ford' ( all auto fills happen through ajax calls for me )

Which is the best event trigger ( keypress , keyup or onchange ) which I should use to achieve this seamlessly without any time lags or sync issues ?

Let me know if I am not clear.

A: 

I would use "focusout" event on the first text box.

$('#text_box_one').focusout(function() {
  //Your auto-complete code goes here.
  //Or preferably replace this inline function with a call to an auto-complete function
});
daviddukeuk
A: 

Try this:

 <input type="text" name="text1" value="whatever" id="text1" />
 <input type="text" name="text2" value="whatever" id="text2" />

Now you can use JQuery like this:

 $("#text1").blur(function(){
    $("#text2").val = $(this).val();
 });

I have used blur event here but you can use corresponding keypress, change as per your requirement.

Sarfraz
why are you invoking val() on text 1 again inside the event handler ? Also keypress on text1 won't help much since it would keep triggering the auto fill on text 2 with every character entered in text 1
vaibhav bindroo
@Akshay: I have fixed it already, by the way what event you think is most appropriate for you?
Sarfraz
and as i said you can use any other elements in my answer as per your requirement.
Sarfraz
ignore my first line as it was for the first revision of your post . But rest of my post still remains valid.
vaibhav bindroo
then how about blur event?
Sarfraz
key press on text 1 would still not do the job for me . keypress on text 2 would although be more efficient for auto fill but then if you are traversing through keys like 'Tab' , 'Shift Tab' doesnt load .
vaibhav bindroo
I suppose blur would do the trick as would onfocus out on text1. Let me see if it is able to handle sync issues or time lag as in case of keypress or keyup.
vaibhav bindroo
@sarfaraz Thanks for the pointers though
vaibhav bindroo