some text
other text
just text
...">

JQuery Sub Selector Question

my html

<div id="x">
  <div id="x1">
    some text
    <div>other text</div>
  </div>
  <div id="x2">just text</div>
<div>

my challenge

I have this element, which can be any jquery selector, 1 or more elements:

var x = $("#x");

now I NEED (this is a plugin that will detect internal elements such as tabs) in order to navigate only through the first elements of the DIV, and not its children. I'm trying to...

$.each($($("div"), x), function() {
  alert($(this).html());
});

shows: 1. some text text 2. other text 3. just text

$.each($($("div"), x).siblings(), function() {
  alert($(this).html());
});

shows: 1. just text 2. text textother

This is the most approximate, but I need it in the correct order. any suggestion? thanks

ps. because it is a plugin that I can not do.

$("#x", "div....")

I need to do it

#(newselector, x)
+5
source share
3 answers

$("#x > div"), , , , , var x:

var x = $('#x');

// this will select only the direct div children of x whatever the x is
x.children('div');
+10

:

$("#x > div")

- div, x.

+1

This method will give you all the inner div text:

x.children('div').each(function() {
  alert($(this).text());
});

If you do not need all the text merged from several child divs, you can only access the direct text node of each of them:

x.children('div').each(function() {
  alert(this.childNodes[0].nodeValue);
});

Note: It is assumed that it xis a jQuery element defined var x = $('#x').

+1
source

All Articles