How to check the type of a child in a div?

I want to check the type of an element that is inside a div. I cannot reference a child. I tried using the following:

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

Where styleTargetis the variable containing the idparent div.

+3
source share
3 answers

There is a difference between a child and a brother. In the example below, all <li>are children <ul>. Each <li>is a child of all the other elements <li>, since they are at 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 use .children()as described here http://api.jquery.com/children/ .

$("."+styleTarget).children().get(0).tagName
+3
source

:

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

.

+2

I think you need to look at a function children()if you do not want to look only at a specific element, and then, as suggested by GenericTypeTea, use :first-childor nth-child.

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

0
source

Source: https://habr.com/ru/post/1754784/


All Articles