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

7
  • 8
    You can't have multiple elements with the same ID, use a class. Commented Jun 13, 2013 at 16:59
  • 1
    stackoverflow.com/questions/1756425/… Commented Jun 13, 2013 at 17:00
  • 2
    The onclick attribute code is running before the jQuery click handler. You can't stop something that has already happened. Commented Jun 13, 2013 at 17:03
  • 1
    I dont wanna remove the onclick attribute becasue the values of the <li> can change. BTW, i updated the answer with 'class' instead 'id' Commented Jun 13, 2013 at 17:10
  • 1
    @MichaelAguilar Then don't use the onclick attribute - store data associated with the element in data-* 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 Commented Jun 13, 2013 at 17:11

3 Answers 3

6

The problem here is the jQuery code runs after the onclick attribute. So there is no way to make it work with the way you currently have it set up. The solution would be to look for the onclick elements and move it to another event, and have the onclick that you add dynamically trigger than event.

$(".foo").each( function() {
   this._onclick = this.onclick;
   this.onclick = null;
});


$(document).on('click','.foo', function(){
    if ($(this).text()!=='a') {
        this._onclick.apply(this,arguments);
    }
});

JSFiddle

The problem with this method is, if the content is added dynamically, you would have to call the code that converts the click.

Sign up to request clarification or add additional context in comments.

1 Comment

a very good answer and solution. I think is the best solution
0

Besides the unique id caveat, just remove the onclick attr:

Markup:

<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>

JS:

$('.foo').map(function(){ 
  if($(this).text() == 'a')
    $(this).removeAttr('onclick')
});

3 Comments

BUT the OP wants it to fire!
But the OP said I want to prevent the default action alert()
@epascarello now I removed it just for the desired element. If user wants to retain a copy of original event, he may do like in the solution you proposed.
-1

You could set a class for your duplicated elements, and use the following snippet:

<ul>
    <li class="foo">a</li>
    <li class="foo">b</li>
    <li class="foo">c</li>
</ul>
<script>
    $('.foo').on('click',function(){
        if($(this).text() === 'a'){
            return false;
        }
        // Whatever you want here.
    });
</script>

1 Comment

You removed the onclick events that are on it and jQuery runs after the onclick that is on the element so your solution fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.