SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Jquery find childnode in ul

    Hi guys,

    I am currently trying to get my script to present two possible dialog boxes on a click handler, depending on whether a particular ul element is populated and I am trying all sorts of things with no success:

    My code is :

    Code:
    		var $gallery = $( "#gallery" ),
    	        $trash = $( "#trash" ); 
    
    		$( "#reorder" ).click(function() {
    
    			if(!$( "ul", $trash ).has("li"))
    			{
    			           trash();	// present the first dialog
    
    			}
    			else 
    			{
    			          no_trash();	//present the second   
    			}
    		});
    Has doesn't work, I have also tried .length() and !=null, am I missing something?

    thanks
    "Persistence is the path to perfection"

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is the context "#trash" the parent element of the ul element? I ask because its hard to determine the problem without HTML code to look at as well.

  3. #3
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Hi there,

    Thanks for your reply,

    Its a <ul> element with the id of trash.

    the link is below:

    Jake Guy - Content Management System

    I have created an image manager as part of my custom cms system and I wish to finish it off by giving the user a different dialog, depending on whether there is an image in the trash or not.

    If you drag a pic into the trash currently, the behavior is the same regardless of whether it is there or not, I am a little stuck , not sure whether I keep getting the selector wrong.

    The only relevant code is the $('#reorder').click handler down the bottom, I really appreciate a second eye on this as using draggable, droppable and sortable has tired me out and now I am probably making silly mistakes !
    "Persistence is the path to perfection"

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Try changing it to the following...

    Code JavaScript:
    $( "#reorder" ).click(function(e) {
        e.preventDefault();
     
        // Normal code here...
    });

  5. #5
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks so much for your reply,

    I had done that originally and was going to put it back in but it still gives 0 as the size0f that list regardless of whether anything is in it or not, if you look now I have changed the code to the following:
    [code]

    var $list = $( "ul", $trash );

    $( "#reorder" ).live('click', function() {

    var $child = $($list > 'li').size();
    alert($child);

    });

    [code]

    I thought that if I used live() that would allow for the fact that it is a droppable element, ie that the element isn't in the DOM when JQuery first processes it.

    It makes no difference, the selector simply isn't working.
    "Persistence is the path to perfection"

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    In your code #trash is a context and also the id of the UL element, if you change...

    Code JavaScript:
    var $list = $( "ul", $trash );
    to
    Code JavaScript:
    var $list = $trash;
    Then it should work fine after that.

  7. #7
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks again but its still not working. Very annoying, as this should be the easy bit :P
    "Persistence is the path to perfection"

  8. #8
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Sorry forgot the other part, change the following...

    Code JavaScript:
    var $child = $($list > 'li').size();
    to
    Code JavaScript:
    var $child = $( 'li', $list ).size();

  9. #9
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have set up another list with the id "test" (bottom of page). There are two elements already inside it so that eliminates the live() thing. The code still doesn't work. Hmmmmmm
    "Persistence is the path to perfection"

  10. #10
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HEY!

    I see we have a ninja among us :P

    That works a treat. What a pain in the ass tho, can you by any chance give me some advice to follow with Jquery and lists in future?

    cheers

    Will Edwards
    "Persistence is the path to perfection"

  11. #11
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Glad to have helped out

    Advice from me , hmm yea that might be a tricky one because i go through a lot of trail and error myself when it comes to lists. Something good though i can think of it reading through the jQuery docs for a few hours as i have and it helps, something else you could do is buy the jQuery novice to ninja book which you can find at jQuery: Novice to Ninja by Earle Castledine and Craig Sharkie.

  12. #12
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,
    Same here trial and error all the way, I have read that book cover to cover, its awesome.

    To be honest, now I'm feeling really dependent on JQuery, not sure if I can remember much traditional JS!

    Forgetting some silly things like the other day I wanted the value of an object and I went something like.

    this.val();

    This didn't work of course because I was trying to get the value of a non-JQuery object and in the end I had to do $(this).val(); !

    Goes to show how dependent you can get LOL
    "Persistence is the path to perfection"

  13. #13
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Been dependent on a JavaScript language isn't a bad thing but it can very distracting if you need to write snippets of native JavaScript and don't remember them. One thing you can do is build up a tiny library snippets and store them on your PC and when you need to use them crack one open and you have a pre ready snippet of code to use.

  14. #14
    SitePoint Evangelist silversurfer5150's Avatar
    Join Date
    Aug 2010
    Posts
    534
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much, I shall take your advice on that, and congratulations of member of the month, have a gd day.

    Will
    "Persistence is the path to perfection"

  15. #15
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Thanks bud


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •