0

How can I get access to the items within "this" so that I can chain further elements onto a "this" object?

I am sure I am not understading something and doing it the long way around, but currently taking a "this" object and manually parsing out the string of items in the element so I can then add further specification

I originally thought I could just:

$(this) + " ul li";

but that just seems to try to add a string to [object HTMLDivElement]

So I then manually extracted and contacenated all the elements together as a string:

function cleanThis(el) { return el.replace(/^ +/,"").replace(/ +$/,"").replace(/  +/g," ") }
function parseThis(el) { return el.tagName + (el.id?"#"+cleanThis(el.id):"") + (el.className?"."+cleanThis(el.className).replace(/ /g,"."):"") }

var eachThis = $(parseThis(this)  + " ul li");

This seems like I am recreating the wheel and that I should be able to access the information I need in JQuery or Javascript

So if $(this) = jQuery(div#nav)

How can I get access to "div#nav" so that I can actually add further specification such as the example above with eachThis (adding on: " ul li")

0

2 Answers 2

3

I think you're looking for .find().

$( this ).find( 'ul li' );

Another way is to provide the containing element as a parameter:

$( 'ul li', $( this ) );
2
  • $('ul li', $(this)); can just be written as $('ul li', this);
    – gen_Eric
    Aug 31, 2011 at 19:26
  • Yes! Thank you one and all, especially Juhana and Rocket. This is exactly what I was trying to find!!
    – verbatim
    Sep 2, 2011 at 16:01
0

You want to add a <ul> to whatever node is inside $(this)?

$(this).append('<ul><li>');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.