Business, Tech, Life, and Whatever else

jQuery pass variables to functions

by Eric Barnes on September 22, 2008

I have been working with jQuery recently because we are going to be using it in the 4.1 release of 68 Classifieds. One of the things I couldn’t get my head around was how to pass the id of a database record to a jQuery click function.

For example in regular javascript I had:

function deleteit(id) {
  alert(id);
}
<a href="#" onclick="deleteid(1)">Delete</a>

So with a little help I found out how it can be accomplished with jQuery:

$('a.delete').click(function(event) {
	event.preventDefault();
	var id = this.id.replace('del_', "");
       alert(id);
}
<a href="delete.php?act=delete&amp;id=1" id="del_1" class="delete">Delete</a>

I know this isn’t anything revolutionary but it sure had me stumped for a little while. Hopefully this will help someone else just learning jQuery.

You May Also Be Interested In...

Optimize and Repair A Database With CodeIgniter
July 8, 2009

Building a news module – 68 Classifieds
July 1, 2009

CodeIgniter Custom Settings File
July 9, 2009

Gravatar Plugin
December 21, 2008

Subscribe Now

If you enjoyed this post, you will definitely enjoy my others. Subscribe to the feed to get instantly updated for those awesome posts soon to come.


  • Dave
    Thanks mate - this part helped me out (I'm a newbie to jQ!)
    $('a.delete').click(function(event)
  • Pietro
    THANK YOU!
  • Lu
    Thanks, jquery is dead simple and yet here I am stumped as well...lol.

    Hey congrats on being a dad, I became a dad 8 months ago,it's a crazy journey...chances are you're changing a diaper right now!

    In your bio, you said a second one is due in Jan 2010, but your daughter was born Dec 2009? YOu gotta explain that one to me! ;-)
  • lol. Yes the next one is due next month so it will but them 13 months apart. Kind of unexpected but they should be best friends growing up. ;)

    Yea kids are amazing and I would have never thought how much they change your life.
  • Thank you! I have been searching for something like this for ages!
    Just something I noticed at the end of the script need to add

    }); instead of just }
  • Thanks for the fix Justin!
  • DDetto
    Thanks, that works! I have been trying to figure this out. Unfortunately it looks like jquery makes everything easy, except getting an id.

    Now that my problem is solved I am exhausted. I think I will have jquery grab me a beer out of the fridge, I hope it can do that :)
  • Bowman
    Ouch, that hZ
  • Sorry you didn't like it. If you have any advice to make it better please let me know.
blog comments powered by Disqus