Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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!

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 591 Times in 572 Posts
    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.
    Last edited by rnd me; 01-09-2009 at 01:06 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    New Coder
    Join Date
    Jan 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank rnd me!!! This works great!!!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •