tags:

views:

71

answers:

4

If I do this

var domElement = $("#id");

and the returned element is a div tag,

How can I then do something like

domElement.$('div a:nth-child(3)').after(somehtml);

This is an example where I want to add some HTML after the third link under that "domElement" div.

The above doesn't seem to work. I have numerous examples where I have already selected a certain element from the entire page HTML and I then want to work inside the "context" of that element.

In 90% of cases I want to continue jQuery selection, traversion and manipulation from a previously selected DOM element from a page not from the whole page like $(..) does.

+1  A: 

Try this:

$('div a:nth-child(3)', domElement).after(somehtml);
egyedg
That works but I think that such context-style selectors have been deprecated. (I can't recall where I saw it, but I think that the jQuery guys prefer the ".find()" function.)
Pointy
I wasn't aware of that, thanks for the info.
egyedg
The context argument hasn't been deprecated. It might be preferable to use find just for personal preference or code clarity, of course (I feel this way, myself). http://api.jquery.com/jQuery/#jQuery1
thenduks
+4  A: 

Two options, use the context argument to the jQuery function:

$('div a:nth-child(3)', domElement).after(something);

or (my preference) use find:

domElement.find('div a:nth-child(3)').after(something);
thenduks
I think OP wants to find the third link within `domElement`, so the selector doesn't need to start with `'div'`
bdukes
@bdukes: I don't think so, his original attempt was `domElement.$('div a:nth-child(3)')`.
thenduks
He said "I want to add some HTML after the third link under that 'domElement' div." I think he thought he had to select `domElement` again.
bdukes
I've got plenty of code that has links inside divs to improve their layout or styling... So, whatever. The accepted answer does it the same way.
thenduks
+6  A: 

You want .find(): http://api.jquery.com/find/

var domElement = $("#id");
domElement.find('div a:nth-child(3)').after(somehtml);

If you plan on chaining this with multiple sub-selectors on domElement, end each use of .find() with .end():

$("#id")
  .find('div')
    .click(function() { alert("#id div"); })
    .find('a.my-class1')
      .click(function() { alert("#id div a.clickable"); })
      .end() // stop working with 'a.my-class1'
    .find('a.my-class2')
      .click(function() { alert("#id div a.my-class2"); }) 
      .end() // stop working with 'a.my-class2'
    .end() // stop working with 'div'
  .click(function() { alert("#id"); });

Conceptually, you're descending deeper into the DOM with .find(), and ascending back up the DOM with .end(). Technically "descending" and "ascending" aren't correct because you can also traverse first up then back down with functions like .parent() or .closest(), both of which can be terminated with .end() to return to the original selector:

$("#id")
  .parent()
    .click(function() { alert("I'm #id's parent!"); })
    .end()
  .click(function() { alert ("back to #id"); });
meagar
This could use some links to docs.
thenduks
+1  A: 

Try

$('div a:nth-child(3)',domElement).after(somehtml);

The context is the second parameter.

Good luck.

cjstehno
I don't know why I answer new unanswered questions... I always submit and find that a bunch of others have posted the same thing. :-)
cjstehno