How to get the child of a element with event.target

Jesus Sandrea

I'm trying to get the child elements of a div to toggle a class 'active'

JS

    const dots = document.querySelectorAll('[data-dots]');
    dots.forEach(dot => dot.addEventListener('click', handleClick));

    function handleClick(e) {
     e.target.getElementsByClassName('tb-drop').classList.toggle('active');
     console.log(e.target.getElementsByClassName('tb-drop'))
    }

HTML

       <div class="dots" data-dots>
         <i class="fas fa-ellipsis-v dots"></i>
         <div class="tb-drop">
            <i class="far fa-edit icon-grid"></i>
            <i class="fas fa-link icon-grid"></i>
         </div>
       </div>

So i'm selecting all the divs with the data-dots attribute and then select the child of that div and add the class active. I tried with e.target.children but didnt work.

Thanks, I'm just trying to learn :)

David says reinstate Monica

In order to identify the first child, the easiest option is simply to use Element.querySelector() in place of Element.getElementsByClassName():

const dots = document.querySelectorAll('[data-dots]');
dots.forEach(dot => dot.addEventListener('click', handleClick));

function handleClick(e) {
  // Element.querySelector() returns the first - if any -
  // element matching the supplied CSS selector (element,
  // elements):
  e.target.querySelector('.tb-drop').classList.add('active');
}

The problem is, of course, that if no matching element is found by Element.querySelector() then it returns null; which is where your script will raise an error. So, with that in mind, it makes sense to check that the element exists before you try to modify it:

const dots = document.querySelectorAll('[data-dots]');
dots.forEach(dot => dot.addEventListener('click', handleClick));

function handleClick(e) {
  let el = e.target.querySelector('.tb-drop');
  if (el) {
    el.classList.add('active');
  }
}

It's also worth noting that EventTarget.addEventListener() passes the this element into the function, so rather than using:

e.target.querySelector(...)

it's entirely possible to simply write:

this.querySelector(...)

Unless, of course, handleClick() is rewritten as an Arrow function.

Demo:

const dots = document.querySelectorAll('[data-dots]');
dots.forEach(dot => dot.addEventListener('click', handleClick));

function handleClick(e) {
  let el = e.target.querySelector('.tb-drop');
  if (el) {
    el.classList.add('active');
  }
}
div {
  display: block;
  border: 2px solid #000;
  padding: 0.5em;
}

i {
  display: inline-block;
  height: 1em;
}

::before {
  content: attr(class);
}

.active {
  color: limegreen;
}
<div class="dots" data-dots>
  <i class="fas fa-ellipsis-v dots"></i>
  <div class="tb-drop">
    <i class="far fa-edit icon-grid"></i>
    <i class="fas fa-link icon-grid"></i>
  </div>
</div>

Or, if you wish to toggle the 'active' class you could, instead, use toggle() in place of add:

const dots = document.querySelectorAll('[data-dots]');
dots.forEach(dot => dot.addEventListener('click', handleClick));

function handleClick(e) {
  let el = e.target.querySelector('.tb-drop');
  if (el) {
    el.classList.toggle('active');
  }
}
div {
  display: block;
  border: 2px solid #000;
  padding: 0.5em;
}

i {
  display: inline-block;
  height: 1em;
}

::before {
  content: attr(class);
}

.active {
  color: limegreen;
}
<div class="dots" data-dots>
  <i class="fas fa-ellipsis-v dots"></i>
  <div class="tb-drop">
    <i class="far fa-edit icon-grid"></i>
    <i class="fas fa-link icon-grid"></i>
  </div>
</div>

References:

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Vue.js + Element UI: Get "event.target" at change

分類Dev

How to target second child element of a div using JAVASCRIPT

分類Dev

How to use event.target.matches to match div element

分類Dev

How to get the element that received the keydown event in TINYMCE

分類Dev

How to search an Event to get the name of an Element?

分類Dev

How to get $scope or $element in an onblur event and run a function in an onblur event?

分類Dev

how to get parent and child text for the kendo menu in the select event

分類Dev

How to configure ESLint for using e.g. event.target and element.parentNode?

分類Dev

How to get the left and top position of a child element of its parent

分類Dev

How to get Element Properties in React Native on a Click Event

分類Dev

Fabric.js dbclick event child element

分類Dev

Appending an element to the child fires the drop event in the parent

分類Dev

Why handleClick event is not fired from Child Element?

分類Dev

Get JSON child element value

分類Dev

LINQ parsing - How do I target the child?

分類Dev

Get Class Name of Element from MouseEvent Target

分類Dev

Get the attr of a dynamically created select child element

分類Dev

Angular 2+: Get child element of @ViewChild()

分類Dev

get child array values in a parent div element

分類Dev

Get last child height of SVG element

分類Dev

Vanilla JS: How to target element by type and class

分類Dev

Get the element (node) which triggered an event

分類Dev

How can I target third child level of parent in jQuery?

分類Dev

How to use XPath to select child text after another child element

分類Dev

How to hide child element using javascript

分類Dev

JQuery Get value of Child Selector for a .on listener for click event

分類Dev

How can I use event.target.id as a jQuery selector?

分類Dev

How do you get the 'Target type' of a shortcut

分類Dev

How can i get the hasClass of click target?

Related 関連記事

  1. 1

    Vue.js + Element UI: Get "event.target" at change

  2. 2

    How to target second child element of a div using JAVASCRIPT

  3. 3

    How to use event.target.matches to match div element

  4. 4

    How to get the element that received the keydown event in TINYMCE

  5. 5

    How to search an Event to get the name of an Element?

  6. 6

    How to get $scope or $element in an onblur event and run a function in an onblur event?

  7. 7

    how to get parent and child text for the kendo menu in the select event

  8. 8

    How to configure ESLint for using e.g. event.target and element.parentNode?

  9. 9

    How to get the left and top position of a child element of its parent

  10. 10

    How to get Element Properties in React Native on a Click Event

  11. 11

    Fabric.js dbclick event child element

  12. 12

    Appending an element to the child fires the drop event in the parent

  13. 13

    Why handleClick event is not fired from Child Element?

  14. 14

    Get JSON child element value

  15. 15

    LINQ parsing - How do I target the child?

  16. 16

    Get Class Name of Element from MouseEvent Target

  17. 17

    Get the attr of a dynamically created select child element

  18. 18

    Angular 2+: Get child element of @ViewChild()

  19. 19

    get child array values in a parent div element

  20. 20

    Get last child height of SVG element

  21. 21

    Vanilla JS: How to target element by type and class

  22. 22

    Get the element (node) which triggered an event

  23. 23

    How can I target third child level of parent in jQuery?

  24. 24

    How to use XPath to select child text after another child element

  25. 25

    How to hide child element using javascript

  26. 26

    JQuery Get value of Child Selector for a .on listener for click event

  27. 27

    How can I use event.target.id as a jQuery selector?

  28. 28

    How do you get the 'Target type' of a shortcut

  29. 29

    How can i get the hasClass of click target?

ホットタグ

アーカイブ