Using jQuery to 'click' a li element

Zabs picture Zabs · Sep 30, 2013 · Viewed 100.1k times · Source

I have a <ul> element that dynamically generates the <li> elements and simply want to run a onclick event

<ul id="results">
    <li class="device_result searchterm" data-url="apple-iphone-5s">
        <a href="#"> Apple iPhone 5s </a>
    </li>
    <li class="device_result searchterm" data-url="apple-iphone-5c">
        <a href="#"> Apple iPhone 5s </a>
    </li>
</ul>

I've got the following jQuery in a $(document).ready block but it doesn't seem to work - any ideas what I'm doing wrong?

$("li .searchterm").click(function() {  
    console.log("testing");
});

Answer

Edorka picture Edorka · Sep 30, 2013

if you add dinamically put the click on the list but select the items:

$("#results").on("click", ".searchterm", function(event){
    console.log('clicked');
});

try on the fiddle: http://jsfiddle.net/emPKS/