Jump to content

Jquery AppendTo Toggle?


shortysbest

Recommended Posts

I want to appendTo a div from a list of names. (like adding them to an email) However I am having troubles with making it so when you click on it the first time it adds them to email, then you click on the same tag (which was appendedTo a div) it appends back to original container (which removes them from email), and so on as many times as the user may do so. Right now the script I use only allows it to do this process twice then it doesn't work, of course if I added more of the events it would do so longer.

 

$(document).ready(function(){
					   

$(".group-video-select-friends li").click(function(){
$(this).appendTo(".group-input-names");

$(".group-input-names li").click(function(){
$(this).appendTo(".group-video-select-friends");

$(".group-video-select-friends li").click(function(){
$(this).appendTo(".group-input-names");


$(".group-input-names li").click(function(){
$(this).appendTo(".group-video-select-friends");
									  });
  });
});
});

 

Notice how it has to be, the next set of clicks needs to be inside the ({ }) tags each time.

 

Link to comment
https://forums.phpfreaks.com/topic/234913-jquery-appendto-toggle/
Share on other sites

You are on the wrong track.  Just for stylistic reasons, I changed the use of classes to id's, but that is not the main problem.  Try this instead:

 

function moveTo(){
  $id = $(this).closest("ul").attr("id");
  if ($id == 'group-video-select-friends') {
    $(this).appendTo("#group-input-names");
  } else {
$(this).appendTo("#group-video-select-friends");
  }
}

$(document).ready(function(){
$("#group-video-select-friends li").click(moveTo);
$("#group-input-names li").click(moveTo);
});

 

Make sure that you're ul's use the id's named.

 

 group-video-select-friends
</pre>
<ul id="group-video-select-friends">
Bob
Ted
Carol
Alice
</ul>
<br><br><h3>group-input-names</h3>
<br><ul id="group-input-names">
</ul

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.