1

I have a situation like this:

HTML

<span class="element e1" id="FirstElement" onclick="Javascript:alert(this.id)"> 
    <span class="element e2" id="SecondElement" onclick="Javascript:alert(this.id)"> ELEMENT </span>
</span>
<button class="addButton"> ADD CLASS </button>

JAVASCRIPT:

$(document).ready(function() {
    $('span.element.anotherClass').click(function(e) {
          alert("ANOTHER ACTION"); 
          return false; // I tried also preventDefault() but not working
    });

    $('.addButton').click(function(e){
        $('span.element').addClass(" anotherClass");
    });

});

Note the two span elements have a onclick action. I want that after adding a class to these elements by clicking on the button, they stop doing the old onclick actions and start doing the new action ( = show the "ANOTHER ACTION" alert).

You can try the code above in this Fiddle.

Hope someone will help me with this problem I can't figure. Thanks

5 Answers 5

1

If you removed the onclick from the HTML then you could do something like this:

$(document).ready(function() {
  $('span.element').click(function(e) {
    if ($(this).hasClass('anotherClass')) {
      alert("ANOTHER ACTION");
    } else {
      alert($(this).attr('id'));
    }
    return false; // I tried also preventDefault() but not working
  });

  $('.addButton').click(function(e) {
    $('span.element').addClass(" anotherClass");
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="element e1" id="FirstElement"> 
    <span class="element e2" id="SecondElement"> ELEMENT </span>
</span>
<button class="addButton">ADD CLASS</button>

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

1 Comment

Yes, It seems to me that this is the best way to do it. Thank you very much
1

jsFiddle Demo

You need to delegate the click event to a selector instead of assigning it once the first time the code runs. This will allow it work for future elements and not just the elements it finds when it is first run.

$('body').on('click','span.element.anotherClass',function(e) {
      alert("ANOTHER ACTION"); 
});

You may also want to make sure you only add the class once. Further, in order to make sure the previous onclick events are removed, you will need to iterate through them and remove the onclick assignment.

$('.addButton').click(function(e){
    $('span.element').not('.anotherClass').each(function(){
      this.onclick = void 0;//void 0 is just undefined
    }).addClass("anotherClass");
});

4 Comments

Ok thank you, but how can I avoid that the two old actions are executed?
Wouldn't it be easier to just call "off" and then "on" for that class' click event? Like $(".Class").off("click"); $(".Class").on("click", functionName);
@TravisJ sorry I don't understand your response.
@Stan - Your code will not remove the event handler. jsfiddle.net/rp87vqeo
0
$(document).ready(function() {
    $('body').on('click','span.element.anotherClass',function(e) {
      alert("ANOTHER ACTION");
        return;
    });

    $('.addButton').click(function(e){        
        $('span.element')[0].onclick = '';
        $('span.element')[1].onclick = '';        
        $('span.element').addClass("anotherClass");

    });

});

Note that this will trigger twice, since you actually have 2 span.element that you're clicking.

Comments

0

Try this - seems a little easier to manage than trying to set universal click events and having to unbind/bind them with every click.

$(document).ready(function(e){
    $(".addButton").on("click", function(e){
        e.preventDefault();
        $("span.element").removeClass("anotherClass"); //Just in case you double-click
        $("span.element").addClass("anotherClass");
        return false;
    });

    $(".element").on("click", function(e){
        e.preventDefault();
        if ($(this).hasClass("anotherClass"))
            alert("another class");
        else
            alert($(this).prop("id"));
        return false;
    });
});

Comments

-1

You can do this:

$('span.element').attr("onclick", yourNewFunction());

1 Comment

That won't work - it'll set the onclick attribute to the return value of their function.

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.