Jeff Hu Jeff Hu - 1 year ago 82
Javascript Question

Assign onfocus to DOM elements in runtime

I am interested in assigning onfocus or other similar functions to a DOM elements in runtime.

General Case:

<input type="text" id="box" onfocus="inputFocused()"/>

Runtime Assignment Case:

<input type="text" id="box"/>

And in the script

$('#box').onfocus = inputFocused();

is nothing more than a simple function. It appears not working properly.

Thanks for any help.

Answer Source

You could use it as :


Hope this helps.


function inputFocused(){
<script src=""></script>
<input type="text" id="box"/>