tomloprod
tomloprod

Reputation: 7892

Avoid event.target refer to the child instead of parent

I'm trying to get data attributes of a group of links and buttons, creating a event listener as follow:

// For all major browsers, except IE 8 and earlier
if (document.addEventListener) {                 
    document.addEventListener("click", executeOnClick);
} else if (document.attachEvent) {   
    // For IE 8 and earlier versions
   document.attachEvent("onclick", executeOnClick);
}

This event listener, executes the next function:

function executeOnClick(e){
    //////////// Only elements which has "specialClass"
    if (hasClass(e.target, 'specialClass')) {
        if(e.target !== undefined){
            console.info(e.target.getAttribute('data-info'));
            e.preventDefault();
        }
    }
}

But doesn't work when the link or button has other tags inside them. Example:

<a data-info="Lorem ipsum 3!" href="#" class="specialClass">
  <div>Link with div inside: <br> "event.target" is "div", not "a"</div>
</a>

I don't know how to get it work when the elements has and has no children. Somebody can help me?

Codepen of my problem: http://codepen.io/tomloprod/pen/gwaVXE

NOTE: I have omitted the definition of the hasClass method because this isn't the problem. Anyway, you can see it on the codepen.

Upvotes: 0

Views: 959

Answers (1)

DavidDomain
DavidDomain

Reputation: 15303

You could use a function which will recursively check the parentNode for the presence of a data-info attribute.

Here is an example.

//////////// This function works well.

function findParentWithData(elem) {
  try {
    if(elem.getAttribute('data-info'))
      return elem;
  } catch(e) {
    console.log('This was an anchor without data-info attribute.')
    return e
  }
  while(!elem.getAttribute('data-info')) {
    return findParentWithData(elem.parentNode);
  }
}

function hasClass(event, className) {
  if (event.classList) {
    return event.classList.contains(className);
  }
  return new RegExp('(^| )' + className + '( |$)', 'gi').test(event.className);
}

function executeOnClick(e) {

  // if click came from body don't do anything
  if (e.target === document.body) return;

  var result = document.getElementById("result");
  result.innerHTML = "";

  //////////// Only elements that has "specialClass"

  // find parent with data-info
  var elem = findParentWithData(e.target)

  if (elem instanceof Element && hasClass(elem, 'specialClass')) {
    if(elem !== undefined){
      result.innerHTML = "Information: " + elem.getAttribute('data-info');
      e.preventDefault();
    }
  }
}

// For all major browsers, except IE 8 and earlier
if (document.addEventListener) {
  document.addEventListener("click", executeOnClick);
} else if (document.attachEvent) {   
  // For IE 8 and earlier versions
  document.attachEvent("onclick", executeOnClick);
}
.btn {
  opacity:0.8;
  border:0;
  -webkit-border-radius: 28;
  -moz-border-radius: 28;
  border-radius: 28px;
  font-family: Arial;
  color: #ffffff;
  font-size: 37px;
  padding: 10px 20px 10px 20px;
  text-decoration: none;
  outline:0;
  margin: 0em 0 1em 0;
  display: -webkit-inline-box;

}
.btn:hover {
  cursor:pointer;
  opacity:1;

  text-decoration: none;
}

.btn.red{
  background:#e74c3c;
}
.btn.green{
  background:#2ecc71;
}
<div id="result"></div>

<a data-info="Lorem ipsum!" href="#" class="btn green specialClass">Link: Working well</a>	

<button data-info="Lorem ipsum 2!" class="btn green specialClass">Button: Working well too</button>

<a data-info="Lorem ipsum 3!" href="#" class="btn red specialClass">
  <div>Link with div inside: <br> Doesn't work</div>
</a>

<a data-info="Lorem ipsum 4!" href="#" class="btn red specialClass">
  <ul>
    <li>   
      Link with ul inside: 
    </li>
    <li>
      Doesn't work
    </li>
  </ul>
</a>

<a href="#" class="btn red">Foo</a>

Upvotes: 1

Related Questions