[jQuery] Re: Attaching an event to a non-existing element?
I would really recommend the listen plugin for jQuery by Aaron Flesler. It's an excellent plugin that also fixes the focus/blur bug in IE6. Have been using it for over two years now (before live() was implemented), and it also have good support for the dblclick event: http://plugins.jquery.com/project/Listen / Johan On 25 Dec, 02:28, Šime Vidas sime.vi...@gmail.com wrote: 3. I have set this response as the content of an empty div. That select element appears well, but maybe it doesn't appear as a DOM member, so that's why I can't attach an event handler to it. If you place HTML code inside an element that is in the DOM tree (like the empty DIV in your example), than the code gets parsed and all HTML elements inside the code are added to the DOM tree... Basically, if you can see it on the page, than it's in the DOM tree I noticed in your code above, you used the wrong name for the method... it's called change, not onchange $('#term2').change(function(){ window.location = http://www.google.com/;; }
[jQuery] Re: Attaching an event to a non-existing element?
My bad for missing that the event was change you could use the 1.4 alpha version of jQuery's .live() though :-) Details about .live() overhaul http://blog.jquery.com/2009/12/04/jquery-14-alpha-1-released/ Latest version http://blog.jquery.com/2009/12/18/jquery-14-alpha-2-released/ On Dec 25, 2:49 pm, Johan Borestad johan.bores...@gmail.com wrote: I would really recommend the listen plugin for jQuery by Aaron Flesler. It's an excellent plugin that also fixes the focus/blur bug in IE6. Have been using it for over two years now (before live() was implemented), and it also have good support for the dblclick event:http://plugins.jquery.com/project/Listen / Johan On 25 Dec, 02:28, Šime Vidas sime.vi...@gmail.com wrote: 3. I have set this response as the content of an empty div. That select element appears well, but maybe it doesn't appear as a DOM member, so that's why I can't attach an event handler to it. If you place HTML code inside an element that is in the DOM tree (like the empty DIV in your example), than the code gets parsed and all HTML elements inside the code are added to the DOM tree... Basically, if you can see it on the page, than it's in the DOM tree I noticed in your code above, you used the wrong name for the method... it's called change, not onchange $('#term2').change(function(){ window.location = http://www.google.com/;; }
[jQuery] Re: Attaching an event to a non-existing element?
.live() to the rescue http://docs.jquery.com/Events/live#typefn On Dec 24, 10:55 am, Octavian Râşniţă orasn...@gmail.com wrote: Hi, I want to set an event handler which is executed after clicking on an element from a combo box, but that combo box doesn't exist when the page is created. It is generated by an AJAX request. I've tried to set an event using: $(document).ready(function(){ $('#term2').onchange(function(){ window.location =http://www.google.com/; } } But this code is not executed, even though I tried to change the selection using the keyboard. Is there a way of setting this event handler if the html element is created later by an AJAX request? Thanks. Octavian
Re: [jQuery] Re: Attaching an event to a non-existing element?
From: MorningZ morni...@gmail.com .live() to the rescue http://docs.jquery.com/Events/live#typefn Thank you, but here is what I read about it: Possible event values: click, dblclick, mousedown, mouseup, mousemove, mouseover, mouseout, keydown, keypress, keyup Currently not supported: blur, focus, mouseenter, mouseleave, change, submit I want to attach an event to a combo box that should be fired when the user double-clicks on it, or clicks on an element of that combo, or selects an element from the keyboard but I think I would need the change event which is not supported. The click event works, but I think it would be fired before the user will reach to choose the wanted element. The double click is listed as supported, but I couldn't make it work. Thank you. Octavian
[jQuery] Re: Attaching an event to a non-existing element?
First, in your code above you set a value to the window.location property... I believe, the value type must be string so this would be valid: window.location = http://www.google.com/;; Second, I haven't heard the term combo box in relation to HTML you mean a SELECT element, right? To answer your question, I don't see why you couldn't attach the event handler after the AJAX response 1. You get the AJAX response 2. You create the combo box 3. You append it to the DOM 4. You attach an event handler to it
Re: [jQuery] Re: Attaching an event to a non-existing element?
From: Sime Vidas sime.vi...@gmail.com Second, I haven't heard the term combo box in relation to HTML you mean a SELECT element, right? Yes. To answer your question, I don't see why you couldn't attach the event handler after the AJAX response 1. You get the AJAX response 2. You create the combo box 3. You append it to the DOM 4. You attach an event handler to it Aha, probably I would need to learn a little more. I have done it this way: 1. I created the AJAX request 2. I received the AJAX response in JSON format, and the content of the response contains the full HTML code for creating the select element starting with select... and ending with /select. 3. I have set this response as the content of an empty div. That select element appears well, but maybe it doesn't appear as a DOM member, so that's why I can't attach an event handler to it. Octavian
[jQuery] Re: Attaching an event to a non-existing element?
3. I have set this response as the content of an empty div. That select element appears well, but maybe it doesn't appear as a DOM member, so that's why I can't attach an event handler to it. If you place HTML code inside an element that is in the DOM tree (like the empty DIV in your example), than the code gets parsed and all HTML elements inside the code are added to the DOM tree... Basically, if you can see it on the page, than it's in the DOM tree I noticed in your code above, you used the wrong name for the method... it's called change, not onchange $('#term2').change(function(){ window.location = http://www.google.com/;; }