Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery on click event not working on list items within unordered list

Tags:

jquery

I have the following list withing an unordered list with id #sortable1, what I want to achieve is whenever a li element is clicked withing < ul id="sortable2"> an onclick event should happen and alert the id from the li element that was clicked. I have one element also within the unordered list that should not be clickable with the class="emptyMessage"

I got stuck and dont know how to proceed

So far I have

<ul id="sortable1" class="connectedSortable ui-sortable">
<li class="ui-state-default" id="1">Name1</li>
<li class="ui-state-default" id="2">Name2</li>
<li class="ui-state-default" id="3">Name3</li>
<li class="ui-state-default" id="4">Name4</li>
<li class="ui-state-default" id="5">Name5</li>
<li style="display: list-item;" class="emptyMessage">No more contacts available</li></ul>

My JQUERY code

$("#sortable1 li").click(function() {
          alert('Clicked list.'+$(this).value);
         });
like image 348
Elitmiar Avatar asked Jan 15 '11 12:01

Elitmiar


2 Answers

This should do it:

$("#sortable1 li").not('.emptyMessage').click(function() {
       alert('Clicked list. ' + this.id);
});

Demo: http://www.jsfiddle.net/gztRq/2/

like image 89
jAndy Avatar answered Nov 17 '22 13:11

jAndy


$("#sortable1 > li.ui-state-default").click(function() {
    alert("Clicked list " + $(this).attr("id"));
});
like image 41
Jimmy Chandra Avatar answered Nov 17 '22 13:11

Jimmy Chandra