user2564649
user2564649

Reputation: 65

jquery replaceWith doesn't work for some reason

I've got this totally simple code for changing one image (#form1) with another (#form2) when clicking on #third, which doesn't seem to work.

I've thought maybe it's because I've already used $(document).ready before but now I'm reading it's fine to use it several times.

So what is wrong? Could someone help me out?

My code:

$(document).ready(function(){ 
    $("#form2").hide();
      $("#third").click(function() { 
        $("#form1").replaceWith("#form2");
      });
    }); 

Upvotes: 1

Views: 1448

Answers (2)

Snake Eyes
Snake Eyes

Reputation: 16764

Because you replace form with a string not with an object

$("#form1").replaceWith($("#form2"));

Upvotes: 3

MrCode
MrCode

Reputation: 64526

Pass the element to .replaceWith(), not the selector string:

$("#form1").replaceWith($("#form2"));
//                      ^^        ^ was missing

The argument to .replaceWith() can be a HTML string, DOM element or jQuery element, but it should never be the selector string like you have.

Upvotes: 3

Related Questions