D
D
Dmitry Tarasov2017-09-19 15:11:48
css
Dmitry Tarasov, 2017-09-19 15:11:48

How to hide child element under parent?

There is such a piece of layout on the page.

<a href="#" class="earn-item__control-item earn-item__control-item-fav earn-item__control-item_favorites">
     <svg class="icon icon-star-empty"><use xlink:href="/images/icons/symbol-defs.svg#icon-star-empty"></use></svg>
</a>

dc29a76dcb9c44dd91d67177fa624fe2.jpg
js code
(function () {
    document.addEventListener("DOMContentLoaded", function (event) {

        var favItems = [].slice.call(document.querySelectorAll(".earn-item__control-item-fav"));

        favItems.forEach(function (elem) {
            elem.addEventListener("click", function (event) {
                var target = event.target;
                event.preventDefault();
                console.log(target);
            });
        });
    });
}());

I want to do the following when clicking on the star, I want to get the parent element a tag a with classes earn-item__control-item earn-item__control-item-fav earn-item__control-item_favorites
But getting the svg tag or use
350ed8ae815c4e0f807c1066edf2ab76.jpg
how to make it show only when clicking on the star tag a.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stepan Krapivin, 2017-09-19
@xevin

use parentNode or parentElement

elem.addEventListener("click", function (event) {
   var target = event.target;
   event.preventDefault();
   console.log(target.parentNode); // <-- выводим в консоль предка с помощью parentNode
});

https://www.w3schools.com/jsref/prop_node_parentel...

R
Roman Dubinin, 2017-10-09
@romash

You already have the a tag in the closure:

(function () {
    document.addEventListener("DOMContentLoaded", function (event) {

        var favItems = [].slice.call(document.querySelectorAll(".earn-item__control-item-fav"));

        favItems.forEach(function (elem) {
            elem.addEventListener("click", function (event) {
                console.log(elem);
            });
        });
    });
}());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question