shadowayex Posted January 13, 2011 Share Posted January 13, 2011 I'm trying to add a focus out on an element that was added dynamically by JQuery.The element was added with this line: $(this).html("<input type=\"text\" value=\"" + text + "\" />"); The above adds the element fine (or at least it looks like it does). Now, I try to do this: $(this).children("input").focusout(function() { alert("foo"); }); And I get $(this).children("input").focusout is not a function. And this: $(this).children("input").bind("focusout", function() { alert("win"); }); Doesn't give me an error, but ti doesn't work either. And to make sure I was getting the element right, I tried this: alert($(this).children("input").val()); And that worked fine.I'm new to JQuery, and I can't seem to figure out what's wrong. Any ideas? Link to comment Share on other sites More sharing options...
thescientist Posted January 13, 2011 Share Posted January 13, 2011 If you aren't doing this already, I would always make sure any code I want executed be defined within the ready method of the document object. In jquery, this would be accomplished like so: $(document).ready(){ //all code} http://api.jquery.com/ready/This makes sure the entire DOM has been loaded before executing code, adding event handlers, defining functions, etc. Link to comment Share on other sites More sharing options...
shadowayex Posted January 18, 2011 Author Share Posted January 18, 2011 If you aren't doing this already, I would always make sure any code I want executed be defined within the ready method of the document object. In jquery, this would be accomplished like so:$(document).ready(){ //all code} http://api.jquery.com/ready/This makes sure the entire DOM has been loaded before executing code, adding event handlers, defining functions, etc. Yep that's being done. Link to comment Share on other sites More sharing options...
Fmdpa Posted January 18, 2011 Share Posted January 18, 2011 Try using the blur method. It is triggered when the focus is removed from the input element. $('myInputElement').blur(function() { //do something onblur}); Link to comment Share on other sites More sharing options...
shadowayex Posted January 18, 2011 Author Share Posted January 18, 2011 Try using the blur method. It is triggered when the focus is removed from the input element.$('myInputElement').blur(function() { //do something onblur}); Blur works. Thank you =D Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.