Chao  LI Chao LI - 2 months ago 6
HTML Question

Why it has been clicked twice?

<li class="nav-parent">
<a><i class="fa fa-list-alt" aria-hidden="true</i><span>Forms</span>
</a>
<ul class="nav nav-children" id="dd">
<li>
<a href="{:url('edit/Form/getone')}"><span class="text"> Editors</span>
</a>
</li>
<li>
<a href="{:url('edit/Form/getone')}"><span class="text"> Editors</span>
</a>
</li>
</ul>
</li>


Hi, as you can see i have a nav bar section, i am using click function, but when i click, it alerts twice. does anyone know why? thanks li

$('li.nav-parent ul li a').click(function(){
alert(1);

});

Answer

I Guess there is a issue in your HTML Construct near

<li class="nav-parent">
   <a><i class="fa fa-list-alt" aria-hidden="true</i><span>Forms</span>
</a>

In above code <i class="fa fa-list-alt" aria-hidden="true</i> is not properly closed.

Try Below one

$(function(){
  $('li.nav-parent ul li a').click(function(e) {
      e.preventDefault();
      alert(1);
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="nav-parent">
  <a>
    <i class="fa fa-list-alt" aria-hidden="true"></i>
    <span>Forms</span>
  </a>
  <ul class="nav nav-children" id="dd">
    <li>
      <a href="{:url('edit/Form/getone')}">
        <span class="text"> Editors</span>
      </a>
    </li>
    <li>
      <a href="{:url('edit/Form/getone')}">
        <span class="text"> Editors</span>
      </a>
    </li>
  </ul>
</li>

Comments