0

I am trying to pass a click event on parent li element to its child a. following is the code I have tried, which does not seem to work. Where did i go wrong? and what is the best way to achieve this.

EDIT I understand I can restyle my a elements and get myself completely out of this situation. But I want to know why the bellow code is not working and what is a propper way to achive this kind of passing of events.

$('.menuItem').click(function() {
  $(this).children('a').trigger('click');
});
ul {
  list-style: none;
  padding: 0px;
  position: relative;
  z-index: 1000;
  display: inline-block;
  background-color: white;
  height: 30px;
}
ul li {
  display: inline-block;
  padding: 20px;
  margin-right: 0px;
  font-weight: 500;
  background-color: white;
  cursor: pointer;
}
ul li:hover {
  background-color: red;
  color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul>
  <li class="menuItem">
    <a href="www.google.com">
      Home
     </a>
  </li>
  <li class="menuItem">
    <a href="www.google.com">
      About
     </a>
  </li>
  <li class="menuItem">
    <a href="www.google.com">
      News
     </a>
  </li>
  <li class="menuItem">
    <a href="www.google.com">
      Gallery
     </a>
  </li>
  <li class="menuItem">
    <a href="www.google.com">
      Media
     </a>
  </li>
  <li class="menuItem">
    <a href="www.google.com">
      Contact
     </a>
  </li>
</ul>
Prakhar Thakur
  • 1,209
  • 3
  • 13
  • 38

1 Answers1

1

Why not do it this way?

$('.menuItem').click(function() {
  var href = $('a', this).attr('href');
  window.location.href = href; //causes the browser to refresh and load the requested url

});
Zorken17
  • 1,896
  • 1
  • 10
  • 16