0

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

Michael Aguilar
  • 766
  • 8
  • 16
  • 1
    @MichaelAguilar Then don't use the `onclick` attribute - store data associated with the element in `data-*` attributes. For example `
  • a
  • `. 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 – Ian Jun 13 '13 at 17:11
  • @Ian the onclick attribute is generated by .NET, I wanna know if there's a simple way to resolve this issue – Michael Aguilar Jun 13 '13 at 17:16
  • @MichaelAguilar Ahh I see. Well I can't imagine it doing it *automatically* (especially for an `alert`), so you really can't modify it happening? – Ian Jun 13 '13 at 17:18