Wednesday, August 10, 2011

Javascript Utilities

function createTag(tag, content, attrs){
	
	var ret = "<" + tag;
	var isFirst = true;
	if(attrs != null){
		for( var k in attrs){
		   if(isFirst){
			   isFirst = false;
			   ret = ret + " " + k + "='";
		   }
		   else{
			   ret = ret + ", " + k + "='";
		   }
		   
		   ret = ret + attrs[k] + "'";
		}
	}
	if(content == null){
		ret = ret + "/>\n";
	}
	else  ret = ret + ">" + content + "\n";
	
	return ret;
}


function createComboBox(options, defaultOption, attrs){
    var ret = "";
	for( var i in options){
		if(defaultOption != null && defaultOption == options[i]){
			ret += createTag("option", options[i], {"selected":"true"});
		}
		else{
			ret += createTag("option", options[i]);
		}
		
	}
	ret = createTag("select", ret, attrs);
	return ret;
};

Reference:                        

No comments:

Post a Comment