JavaScript Javascript: getElementsByClassName: how to get only parents getelementsbyclassname javascript only

I have html like this:

<div class="item">
    <img class="item-image" src="${item.getImage()}"/>
    <p>${item.getName()}</p>
</div>

and Javascript:

var classname = document.getElementsByClassName("item");

for (var i = 0; i < classname.length; i++) {
    classname[i].addEventListener('click', (e) => {
        addBorder(e.target);

    });
};

function addBorder(item) {
    if (item.tagName = 'DIV') {
        item.style.border = "3px solid red";
    }
}

When I click on item, it adds a red border. However if the click happened to touch the image or the paragraph, the red border is drawn around them. I tried to prevent it by adding the if condition inside addBorder, but it didn't help. Is there a way to only make the parent div red-bordered, even if the click happened to land inside p or img?

enter image description here

Answer:1

I have below array how to get list of names where the age is greater than 18. so the out put should be: ["Anna", "Bob"] friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, ...

I have below array how to get list of names where the age is greater than 18. so the out put should be: ["Anna", "Bob"] friends = [{ name: 'Anna', books: ['Bible', 'Harry Potter'], age: 21 }, ...

  1. array from properties file
  2. array properties in javascript
  3. array properties in c#
  4. array properties in java
  5. array properties in js
  6. array properties in python
  7. array properties in php
  8. array properties in swift
  9. array properties in vba
  10. array properties in data structure
  11. array properties in typescript
  12. array properties in angular 2
  13. array properties in angular 6
  14. array properties in vb.net
  15. array properties in matlab
  16. array properties in powershell
  17. array in properties file spring
  18. array in properties file java
  19. array of properties
  20. array in properties spring

When I click more button my div 1 is replacing div 2. Most of the time div2 contains lots of text, and requires users to scroll down in order to read the entire post. The problem is that by clicking ...

When I click more button my div 1 is replacing div 2. Most of the time div2 contains lots of text, and requires users to scroll down in order to read the entire post. The problem is that by clicking ...

I have some squares sliding down the page, in a chained promise fashion: https://jsfiddle.net/u4x0qwfo/3 The code is: new Promise(function(resolve, reject) { $("#shape").css({ top: 100 }); ...

I have some squares sliding down the page, in a chained promise fashion: https://jsfiddle.net/u4x0qwfo/3 The code is: new Promise(function(resolve, reject) { $("#shape").css({ top: 100 }); ...

  1. javascript promise returned is ignored
  2. javascript promise returned
  3. js promise returned is ignored
  4. js return promise
  5. javascript handle returned promise

I have many dinamic tables, each one creates a dinamic master checkbox inside an input and several checkboxes inside td's (each one has a different ID). The idea is once I check or uncheck the master ...

I have many dinamic tables, each one creates a dinamic master checkbox inside an input and several checkboxes inside td's (each one has a different ID). The idea is once I check or uncheck the master ...

  1. check all checkboxes inside div jquery
  2. check all checkboxes inside a div