Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Adding to an element's event

Tags:

jquery

Is it possible in jQuery to add an element's event? For example if I wanted to add a .fadeIn() to an element when it is clicked but also retain any other functionality that may already exist (given that I do not know if there is or isn't currently anything firing on click).


1 Answers

jQuery doesn't overwrite events, it adds to them. So if you did:

$(document).ready(function() {
    $("#test").click(function () { $(this).append("test<br />"); });
    $("#test").click(function () { $(this).append("test 2<br />"); });
});

with a div looking like:

<div id="test">click me <br /></div>

When you click on the div, it will append "test" and "test 2" at the same time.

like image 103
Brandon Montgomery Avatar answered Jan 02 '26 19:01

Brandon Montgomery



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!