Opposite jQuery.remove

I have an input form that I am trying to remove / add to a click function.

Here is my code:

<script> $(document).ready(function(){ $("#1").click(function () { $("#1").hide(""); }); $("#1").click(function () { $("#twitter").remove(); }); $("#1").click(function () { $("#2").show(""); }); $("#2").click(function () { $("#2").hide(""); }); $("#2").click(function () { $("#1").show(""); }); }); </script> <span id="1"> <img align="left" src="../images/twitter_blue.png" width="30px;" style="margin:5px;"/> <div>Share as @<?php echo $_SESSION['username'] ?></div> </span> <img align="left" id="2" src="../images/twitter_white.png" width="30px;" style="margin:5px;display:none;"/> <input id="twitter" type="hidden" name="twitter" value="yes"/> 

You can see that when you click on the space id = "1" it deletes the image and the input form. And when you click on img id = "2", it hides it and returns the original image. How can I return or add an input form that I initially delete when I click img id = "2". Is there a function opposite of .remove?

Thanks!

+4
source share
3 answers

Use detach if you need to keep existing jQuery data :

 var twitpar = $('#twitter'); var twit = $('#twitter').detach(); 

then reconnect:

 twitpar.append(twit); 
+4
source

you can .clone () whatever you want

you can replace with () whatever you want

;)

+1
source

you have such a strange case and code here.

After removing it, you must add it or add it again.

  $(document).ready(function(){ var $one = $('#1'); var $two = $('#2'); var $twitter = $('#twitter'); $one.click(function () { $one.hide(); $two.show(); $twitter.remove(); }); $two.click(function () { $one.show(); $two.hide(); $twitter.parent().append('<input id="twitter" type="hidden" name="twitter" value="yes"/>'); }); }); 

instead of "hard coding" the element, you can use a template, for example, a jQuery or mustache.js template, etc.

I usually use iCanHaz. so the code could be like that

 $twitter.parent().append(ich.twitter({value: 'yes'}); 

and you define twitter in

 <script id="twitter" type="text/html"> <input id="twitter" type="hidden" name="twitter" value="{{value}}"/> </script> 
0
source

All Articles