tags:

views:

99

answers:

2

Hi there, I'm trying to pass a value to an input box via json. I've been using:

$.getJSON(('somedata.json', function(json){    
    $('#myinput').val(json.values);
});

Works fine. But I needed it to look as if someone focused the input box, typed the json.values inside and then blurred it, because my input box does all sorts of stuff afterwards. Is it possible to do this via script?

A: 

I'm not sure if I understood you correctly, but for simulating focus and blur, this is as close you'll get:

$.getJSON(('somedata.json', function(json){    
    $('#myinput').focus().val(json.values).blur();
});
Tatu Ulmanen
You beat me by eleven seconds.
SLaks
This will actually focus and then blur the `input` element. You could use `.triggerHandler('focus').val(...).triggerHandler('blur')` to just trigger the events without the associated browser actions and bubbling.
Doug Neiner
Actually, this won't focus the element. Read the documentation: http://docs.jquery.com/Events/focus
SLaks
A: 

Your question is unclear.

If you want to run the code that's in the focus and blur events, you can trigger the events like this:

$.getJSON(('somedata.json', function(json){    
    $('#myinput').focus().val(json.values).blur();
});
SLaks