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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Cleaning up a function

    Code:
    function GetSubmitables( obj ){
    	url = '';
    	inputs = obj.getElementsByTagName('INPUT');
    	selects = obj.getElementsByTagName('SELECT');
    	textarea = obj.getElementsByTagName('TEXTAREA');
    	if( inputs.length > 0 ){
    		for(z=0; z<inputs.length; z++){
    			if( inputs[z].name.indexOf('[]') >= 0 ){
    				url += '&'+inputs[z].name.replace('[]','')+'%5B%5D='+escape(inputs[z].value);
    			} else {
    				url += '&'+escape(inputs[z].name)+'='+escape(inputs[z].value);
    			}
    		}
    	}
    	if( selects.length > 0 ){
    		for(z=0; z<selects.length; z++){
    			if( selects[z].name.indexOf('[]') >= 0 ){
    				url += '&'+selects[z].name.replace('[]','')+'%5B%5D='+escape(selects[z].value);
    			} else {
    				url += '&'+escape(selects[z].name)+'='+escape(selects[z].value);
    			}
    		}
    	}
    	if( textarea.length > 0 ){
    		for(z=0; z<textarea.length; z++){
    			if( textarea[z].name.indexOf('[]') >= 0 ){
    				url += '&'+textarea[z].name.replace('[]','')+'%5B%5D='+escape(textarea[z].value);
    			} else {
    				url += '&'+escape(textarea[z].name)+'='+escape(textarea[z].value);
    			}
    		}
    	}
    	return url;
    }
    This function gets all of the INPUT, SELECT, and TEXTAREA objects inside a given container and adds thier name and value to an AJAX url.

    The problem is that the results from getElementsByTagName() is not a true array, and to my knowledge cannot be concatenated, hence requireing the redundant code.

    Can someone help me get this all in one loop, or better yet, make getElementsByTagName() get multiple tags, or concat the 3 lists together?

    Thank you.
    If I'm postin here, I NEED YOUR HELP!!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,074
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You can do it like this:
    Code:
    function GetSubmitables( obj ){
    	var url = '';
    	var inputs = obj.getElementsByTagName('INPUT');
    	var selects = obj.getElementsByTagName('SELECT');
    	var textarea = obj.getElementsByTagName('TEXTAREA');
    	url += getParam(inputs);
    	url += getParam(selects);
    	url += getParam(textarea);
    	return url;
    }
    
    function getParam(els){
    	var param="";
    	for (var z=0; i<els.length; z++){
    		if( els[z].name.indexOf('[]') >= 0 ){
    			param += '&'+els[z].name.replace('[]','')+'%5B%5D='+escape(els[z].value);
    		} else {
    			param += '&'+escape(els[z].name)+'='+escape(els[z].value);
    		}
    	}
    	return param;
    }
    The code still loops 3 times but the it is more manageable and reusable.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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