tags:

views:

43

answers:

3

I want to check the type of an element which is placed inside a div. I'm not able to reference the child element. I have tried using the following:

$("#"+styleTarget).siblings(0).get(0)tagName

Where styleTarget is a variable holding the id of the parent div.

A: 

Hi there.

I think you need to be looking at the children() function, unless you want to only look at a specific item, then, as GenericTypeTea suggested, use the :first-child or nth-child selectors.

http://api.jquery.com/children/

Cheers.

Jason Evans
+2  A: 

You can get it like this:

var tagName = $("#"+styleTarget+" *")[0].tagName; //or...
var tagName = $("#"+styleTarget+" :first-child")[0].tagName;

The important part is the space between the selectors.

Nick Craver
You don't need a space between the first-child filter.
GenericTypeTea
@GenericTypeTea - you do. Think about the difference between `li:first-child` and `ul :first-child`.
Kobi
@GenericTypeTea - Yes, you do - your test is a bit flawed, here's your code, with the child *not* being a div: http://jsfiddle.net/nKTTB/ It alerts `DIV` because the *parent* is a `<div>` and it's a `:first-child` of body, but you're selecting the parent, not the child without a space.
Nick Craver
@All - Ahhh crapsticks. I stand corrected.
GenericTypeTea
+1  A: 

There's difference between a child and a sibling. In the example below, all the <li> are childrens of <ul>. Each <li> is a sibling of all of the other <li> elements, because they are on the same level in the DOM tree.

<ul class="parent">
  <li>List 1</li>
  <li>List 2</li>
  <li>List 3</li>
</ul>

You should be using .children(), as documented here http://api.jquery.com/children/.

$("#"+styleTarget).children().get(0).tagName
Erik Töyrä