0

This is jquery, code, it always returns ID = 1 when you click on "GENERAL" or inner section like "CSS ISSUES". While i am trying to get ID of the Li clicked.

 <ul id="faq_list" class="list-unstyled">
       <li data-type="parent" data-value="1">
                <a href="javascript:;">General</a>
                <div class="subdata" id="target_1">
                   <ul>             
                        <li data-type="parent" data-value="5">
                      <a href="javascript:;">CSS Issues</a>
                      <div class="subdata" id="target_5"></div>
                      </li>
                  </ul>
                </div>
       </li>
       </ul>

Here is JQUERY CODE

    $(document).ready(function(e) {

        $("li").on('click',function(e) {


            var id = $(this).attr('data-value'); alert(id);
            load_data(id);
        });
    });

1 Answer 1

1

You need to stop event bubbling using stopPropagation():

$("li").on('click',function(e) {
    e.stopPropagation();

    var id = $(this).attr('data-value');
    alert(id);
    load_data(id);
});

See this JSFiddle, or run the following snippet for a demo.

$("li").on('click', function(e) {
  e.stopPropagation();

  var id = $(this).attr('data-value');
  alert(id);
  load_data(id);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="faq_list" class="list-unstyled">
  <li data-type="parent" data-value="1"> <a href="javascript:;">General</a>

    <div class="subdata" id="target_1">
      <ul>
        <li data-type="parent" data-value="5"> <a href="javascript:;">CSS Issues</a>

          <div class="subdata" id="target_5"></div>
        </li>
      </ul>
    </div>
  </li>
</ul>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank You, Actually i had tried e.stopPropagation() earlier, but couldn't make it work.
This code will not work with ajax returned data, that was actually my problem. e.stopPropagation()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.