Jquery focus on append element

I want to focus an element with Jquery created by an append. My exemple :

$("#Collaps").append('<a id="link_tuiles_1" href="test.php">Link</a>')

$("#link_tuiles_1").focus(function(){       
alert('ok');   
});

<div id="Collaps"></div>

But it doesn't works. Could you help me ?

Answers 1

  • It will not work because you bind the function before the element is inserted on page, try it like this

    ("#Collaps").append('<a id="link_tuiles_1" href="test.php">Link</a>')
    
    $(document).on("focus","#link_tuiles_1",function(){       
     alert('ok');   
    });
    

Related Articles