Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2009, 10:20 PM   PM User | #1
gsprague
New Coder

 
Join Date: Jan 2009
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
gsprague is an unknown quantity at this point
Turn 3 .getjson() into 1

Hello All,

I am a newbie when it comes to JQuery and was wonder the best way to get this into one click.function(). Or would it be best to leave it as 3? Basically the only difference in the 3 click.functions() is the href id which I am passing to my .getJSON url. This is probably really simple but I'm not sure of the best way, maybe by getting the href id via .this somehow?
Code:
	$(document).ready(function() {
		$('a#clients').click(function(){
			//$('#ulist li').remove();
			$('#ulist li').hide("slow");
			$.getJSON("select_from_db.php?table=clients",
			function(data){
				for (var i=1; i < data.length; i++){
					var dataRow = (data[i]['id']+' '+data[i]['name'])
					$('<li>' + dataRow + '</li>').appendTo('ul');
				}
			    //alert(data[i]['id']);
			});
		});
		$('a#hardware').click(function(){
			$('#ulist li').hide("slow");
			$.getJSON("select_from_db.php?table=hardware",
			function(data){
				for (var i=1; i < data.length; i++){
					var dataRow = data[i]['id']+' '+data[i]['model']
					$('<li>' + dataRow + '</li>').appendTo('ul');
				}
			    //alert(data[i]['id']);
			});
		});
		$('a#software').click(function(){
			$('#ulist li').hide("slow");
			$.getJSON("select_from_db.php?table=software",
			function(data){
				for (var i=1; i < data.length; i++){
					var dataRow = data[i]['id']+' '+data[i]['manufacturer']
					$('<li>' + dataRow + '</li>').appendTo('ul');
				}
			    //alert(data[i]['id']);
			});
		});
	});
Any help is greatly appreciated!
Thanks you very much!
gsprague is offline   Reply With Quote
Old 01-09-2009, 01:03 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
i don't know if there is a best way, but you might try something like this:

Code:
$(document).ready(function() {

 
   function clicker(addy, cat){// builds an onclick function that closes clicker's arguments
		return function(){
				//$('#ulist li').remove();
				$('#ulist li').hide("slow");
				$.getJSON(addy,
				function(data){
					for (var i=1, mx=data.length; i < mx; i++){
						var dataRow = (data[i]['id']+' '+data[i][cat])
						$('<li>' + dataRow + '</li>').appendTo('ul');
					}
				    //alert(data[i]['id']);
				});
			}
   }

	$('a#clients').click(clicker("select_from_db.php?table=clients", 'name' ));
	$('a#hardware').click(clicker("select_from_db.php?table=hardware",'model' ));
	$('a#software').click(clicker("select_from_db.php?table=software", 'manufacturer'));

});
basically i am just using a an extra function as a template to give me a couple keywords to use privately in the onclick.
the vars addy and cat are set at build time, and the inner onclick function remembers those arguments, even though it doesn't accept any arguments of its own.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%

Last edited by rnd me; 01-09-2009 at 01:06 PM..
rnd me is offline   Reply With Quote
Old 01-14-2009, 09:35 PM   PM User | #3
gsprague
New Coder

 
Join Date: Jan 2009
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
gsprague is an unknown quantity at this point
Thank rnd me!!! This works great!!!!
gsprague is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:02 PM.


Advertisement
Log in to turn off these ads.