What I said about the handler being attached more than once really  
only applied to the example we were working with. Considering you are  
emptying the content now the previous .test will not exist by the  
time you load the new one. Also, Jörn's advise about using find() is  
good practice and will ensure that the handler is attached only to  
the .test elements it "finds" within the string you just appended.  
You should be ok with something like:

function        testHandler() {
        $(".test").change(function(){
                alert(this.options[this.selectedIndex].text);
        });
}

        $(document).ready(function(){
                testHandler();
                $(".trigger.fruits").click(function(){
                        $("#content").empty();
                        $("#content").append('<select name="fruits" 
class="test"><option
value="first">apple</option><option
value="second">orange</option><option
value="third">peach</option></select>')
                                .find(".test").each(testHandler);
                        return false;
                });
                $(".trigger.vegetables").click(function(){
                        $("#content").empty();
                        $("#content").append('<select name="vegetables"
class="test"><option value="first">tomato</option><option
value="second">potatoe</option><option
value="third">carrot</option></select>')
                                .find(".test").each(testHandler);
                        return false;
                });
        });

I'm pretty sure that code can be optimised better than this though.
Also, about the parsing of the HTML - jQuery does do this for you so  
don't worry about that.

Good luck
Joel.


On 24/01/2007, at 12:34 AM, Laurent Goussard wrote:

> If I well understood Joel and Jörn, I am suppose to declare my event
> handler each time I switch the #content's content, but this will
> result to increment the alert() number each time the event is
> declared... What a pity...
>
> I cannot really understand why jQuery does not add the content newly
> loaded to the existing DOM. I mean jQuery should parse and update the
> DOM foreach .html method used (in append*, load, etc...), shouldn't it
> ?
>
> I can't imagine I'm the only one who wanted to add jQuery events on
> html content loaded with jQuery. Am I ?


_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to