-4

jQuery does not seem to be able to select a loaded HTML element. Here is my HTML:

<div class="class name"  style="display: none;">
    <div id="submenuID" class="submenuID" />
    <script>
        loadHtmlUsingJavascript('parameter1', 'parameter2');
    </script>
</div>

The loaded HTML is a list of links. This is my JavaScript:

$("#submenuID li").addClass("active");

But it would not add the class. After some snooping around. I learn to use

$("#submenuID li").live('click', function() {
    $("#submenuID li").addClass("active");
});

However, it does not work until I click on the link a second time.

wjl
  • 7,143
  • 1
  • 30
  • 49
SantoshGupta7
  • 5,607
  • 14
  • 58
  • 116
  • `live` needs to be called on an element that is static on the page. You could fix your problem by doing `$(document).live` or possible `$("#submenuID").live()`. However, that's deprecated, use the answer that we've linked to when closing your question, which is what Jorge Silva suggested http://stackoverflow.com/a/25772585/227299 – Ruan Mendes Sep 10 '14 at 19:09

1 Answers1

1

You have to do the following:

$("#submenuID").on('click', 'li', function() {
    $(this).addClass("active");
});

First, the jQuery .live function is deprecated since jQuery 1.7 (http://api.jquery.com/live/)

Also, you should be listening for li methods inside the #subMenuID element.

Jorge Silva
  • 4,574
  • 1
  • 23
  • 42