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 :


		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

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.

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 !

Try changing it to the following…

$( "#reorder" ).click(function(e) {
    e.preventDefault();

    // Normal code here...
});

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:



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

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

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


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.

In your code #trash is a context and also the id of the UL element, if you change…

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

to

var $list = $trash;

Then it should work fine after that.

Hi thanks again but its still not working. Very annoying, as this should be the easy bit :stuck_out_tongue:

Sorry forgot the other part, change the following…

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

to

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

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

HEY!

I see we have a ninja among us :stuck_out_tongue:

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

Glad to have helped out :slight_smile:

Advice from me :rolleyes:, 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.

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

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.

Thanks very much, I shall take your advice on that, and congratulations of member of the month, have a gd day.

Will

Thanks bud