jQuery .On Pass Sender Information to Handler Function

I needed to display a modal box that would pass information back to the link (sender) that was clicked. In my experience this has been handled in the past by either:

1) Creating an individual modal box for each element that would need to one, and display it when the link is clicked
2) Using a generic object to pass data back and forth from the modal to the sender

I didn’t really like either of these approaches from an efficiency/readability standpoint. Fortunately, jQuery 1.7 introduced the .On function which allows you to delegate events to their respective handlers.

My scenario is a color-picker. I only have one instance of the palette (modal) which handles all requests from senders. Here’s the code block that accomplishes everything.

 
<script type="text/javascript">
$(document).ready(function() { 
 
	$(".show").click(function(e) {
		//remove binding (off) first before binding to prevent handler from being called more than once
			$('div').off('click').on('click','a',e.target, function(e){
				//e.target passed as a parameter to handler function
				//tells us which link was clicked 
				//and which is the owner we want to update
				var sender_id = e.data.id;
				//this e.target refers to the ahref that was clicked in the div
				var color_selected = $(e.target).attr('title');
				$("#" + sender_id).css({color: color_selected }); 
		});
	return false;
	});
 
});
</script>

And the code in action:
DEMO

5 thoughts on “jQuery .On Pass Sender Information to Handler Function”

  1. Good day

    SEO Link building is a process that requires a lot of time fo joe-riggs.com
    If you aren’t using SEO software then you will know the amount of work load involved in creating accounts, confirming emails and submitting your contents to thousands of websites in proper time and completely automated.

    With THIS SOFTWARE the link submission process will be the easiest task and completely automated, you will be able to build unlimited number of links and increase traffic to your websites which will lead to a higher number of customers and much more sales for you.
    With the best user interface ever, you just need to have simple software knowledge and you will easily be able to make your own SEO link building campaigns.

    The best SEO software you will ever own, and we can confidently say that there is no other software on the market that can compete with such intelligent and fully automatic features.
    The friendly user interface, smart tools and the simplicity of the tasks are making THIS SOFTWARE the best tool on the market.

    IF YOU’RE INTERESTED, CONTACT ME ==> seosubmitter@mail.com

    Regards,
    Melba

  2. Hi

    SEO Link building is a process that requires a lot of time fo joe-riggs.com
    If you aren’t using SEO software then you will know the amount of work load involved in creating accounts, confirming emails and submitting your contents to thousands of websites in proper time and completely automated.

    With THIS SOFTWARE the link submission process will be the easiest task and completely automated, you will be able to build unlimited number of links and increase traffic to your websites which will lead to a higher number of customers and much more sales for you.
    With the best user interface ever, you just need to have simple software knowledge and you will easily be able to make your own SEO link building campaigns.

    The best SEO software you will ever own, and we can confidently say that there is no other software on the market that can compete with such intelligent and fully automatic features.
    The friendly user interface, smart tools and the simplicity of the tasks are making THIS SOFTWARE the best tool on the market.

    IF YOU’RE INTERESTED, CONTACT ME ==> seosubmitter@mail.com

    Regards,
    Star

  3. Howdy

    I Will Provide 20.000 Backlinks From Blog Comments for joe-riggs.com,
    By post 400k blog comments from where you will receive at least 20.000 live links.

    – Use unlimited URLs
    – Use unlimited keywords (anchor text)
    – All languages supported
    – Link report included

    Boost your Google ranking, get more traffic and more sales!

    IF YOU ARE INTERESTED
    CONTACT US => lisaf2zw526@gmail.com

  4. Good day!

    You Need Leads, Sales, Conversions, Traffic for joe-riggs.com ? Will Findet…

    I WILL SEND 5 MILLION MESSAGES VIA WEBSITE CONTACT FORM

    Don’t believe me? Since you’re reading this message then you’re living proof that contact form advertising works!
    We can send your ad to people via their Website Contact Form.

    IF YOU ARE INTERESTED, Contact us => lisaf2zw526@gmail.com

    Regards,
    Mortlock

Leave a Reply

Your email address will not be published.