GG.
GG.

Reputation: 21854

Replace the content of a div several times

I have a select with my servers and I load information on the selected server without reloading the page. I am using ajax and ReplaceWith().

I tried using live() to replace the information several times, but it only works once, why?

<script>
    $(function(){
        $('select').live('change', function(){
            $.ajax({
                type: "POST",
                url: "server.php",
                data: "hostname=" + $(this).val(),
                success: function(data){
                    $("#results").replaceWith(data);
                }
            })
        });
    });
</script>

Upvotes: 0

Views: 153

Answers (3)

Kjetil Watnedal
Kjetil Watnedal

Reputation: 6167

It is because you are replacing the #results container with the data. The next time the $("#results") selector will not match any elements (because the container was replaced by the previous call).

.html() does not replace the container, but updates the content of the container.

Upvotes: 2

GG.
GG.

Reputation: 21854

I do not really understand why it works with html() and not with ReplaceWith(), but it works!

<script>
    $(function(){
        $('select').live('change', function(){
            $.ajax({
                type: "POST",
                url: "serveur.php",
                data: "hostname=" + $(this).val(),
                success: function(data){
                    $("#results").html(data);
                }
            })
        });
    });
</script>

Sorry to answer my own question.

Upvotes: 1

Gfox
Gfox

Reputation: 307

live() method is kind of deprecated and might work not properly. Try on() instead of.

Upvotes: 0

Related Questions