I have this code:
<ul>
<li class="foo" onclick="alert('Hello')">a</li>
<li class="foo" onclick="alert('Hello')">b</li>
<li class="foo" onclick="alert('Hello')">c</li>
</ul>
I want to prevent the default action alert() when click in <li> with text "a", and I use this code:
$(document).on('click','.foo', function(){
if ($(this).text()=='a')
return false;
});
But this doesnt work: http://jsfiddle.net/Trk6W/10/
Is there a solution only modifying the javascript?
Updated: I dont wanna remove the attribute onclick because the values of li can change
onclickattribute code is running before the jQuery click handler. You can't stop something that has already happened.<li>can change. BTW, i updated the answer with 'class' instead 'id'onclickattribute - store data associated with the element indata-*attributes. For example<li class="foo" data-something="whatever">a</li>. You can then get the stored value with$(element).attr("data-something")or$(element).data("something"). Don't try and mix basic and jQuery event handling