+ Reply to Thread
Results 1 to 6 of 6

Thread: jQuery remove() and append() dilemma

  1. #1
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    jQuery remove() and append() dilemma

    I didn't exactly know how to phrase my problem, so i just wrote dilemma. :]
    http://jsfiddle.net/imHavoc/kGKJJ/9/

    My Dilemma:
    • When you 'delete' any li from the Board or Admin it should go to Uncategorized. Good.
    • When you 'delete' from Uncategorized, it should remove the li entirely. So-so.
    The native li's in Uncategorized follow the function, but any li that comes from Board or Admin still follows its original function and gets appended to the end of the list.

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: jQuery remove() and append() dilemma

    You also have a problem with moving Uncategorized to Admin and then deleting. It removes that li when it probably should move it back to Uncat.

    click handler should check the div's parent's parent's id. If it is "uncategorized", then delete. Otherwise move to "uncategorized"
    Nothing is always absolutely so.

  3. #3
    diabolo's Avatar
    diabolo is offline Community Advocate diabolo is on a distinguished road
    Join Date
    Nov 2007
    Location
    Jersey Shore
    Posts
    1,683

    Re: jQuery remove() and append() dilemma

    Thank you descalzo! It was silly of me not to think of using if else statements.

    I updated the code for anybody that needs a reference:
    http://jsfiddle.net/imHavoc/kGKJJ/11/

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: jQuery remove() and append() dilemma

    Instead of walking up the DOM tree and checking the ID of a specific ancestor, you could use closest or parents to check whether any ancestor has the ID of interest.

    Also, rather than checking the ancestor yourself, you can use live to dynamically match the element selector a listener is bound on.

    Code:
        jQuery('#manage div.remove').live('click', function() {
            jQuery('ul#uncategorized').append(jQuery(this).parent());
        });
    
        jQuery('#uncategorized div.remove').live('click', function() {
            jQuery(this).closest('li').remove();
        });
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    callumacrae's Avatar
    callumacrae is offline not alex mac callumacrae is just really nice
    Join Date
    Dec 2007
    Location
    Wellesbourne, England
    Posts
    5,162

    Re: jQuery remove() and append() dilemma

    Quote Originally Posted by misson View Post
    Instead of walking up the DOM tree and checking the ID of a specific ancestor, you could use closest or parents to check whether any ancestor has the ID of interest.

    Also, rather than checking the ancestor yourself, you can use live to dynamically match the element selector a listener is bound on.

    Code:
        jQuery('#manage div.remove').live('click', function() {
            jQuery('ul#uncategorized').append(jQuery(this).parent());
        });
    
        jQuery('#uncategorized div.remove').live('click', function() {
            jQuery(this).closest('li').remove();
        });
    Are there any languages you don't know?

    ~Callum
    I can customise your phpBB board. Send me a PM.
    lynxphp - info, tutorials and scripts
    "A forum post should be like a skirt; long enough to cover the subject but short enough to keep things interesting."

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: jQuery remove() and append() dilemma

    Snobol (sadly), Cobol (happily).
    Last edited by misson; 02-18-2011 at 03:36 PM.

+ Reply to Thread

Similar Threads

  1. jQuery & CSS Help...
    By toddart65 in forum Programming Help
    Replies: 7
    Last Post: 05-16-2010, 10:09 PM
  2. Replies: 13
    Last Post: 03-27-2009, 07:25 PM
  3. flash dilemma
    By stalkio in forum Off Topic
    Replies: 2
    Last Post: 08-21-2008, 06:19 AM
  4. Append XML File using PHP
    By akv003 in forum Programming Help
    Replies: 9
    Last Post: 07-31-2008, 06:10 AM
  5. How to Append new Child in XML.
    By oracle in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 12-26-2007, 09:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers