[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Tips: javascript code to let you add form list from child to parent II



 
    
javascript code to let you add form list from child to parent II:

<HTML>
<HEAD>

<SCRIPT LANGUAGE="JavaScript">
function small_window (myurl) {
	var newWindow;
	var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=300,height=200';
	newWindow = window.open (myurl, "Add_from_Src_to_Dest", props);
}

// Adds the list of selected items selected in the child
// window to its list. It is called by child window to do so.  
function addToParentList (sourceList) {
	destinationList = window.document.forms[0].parentList;
	for (var count = destinationList.options.length - 1; count >= 0; count--) {
		destinationList.options[count] = null;
	}
	for (var i = 0; i < sourceList.options.length; i++) {
		if (sourceList.options[i] != null)
		destinationList.options[i] = new Option (sourceList.options[i].text, sourceList.options[i].value );
   }
}

// Marks all the items as selected for the submit button.  
function selectList (sourceList) {
	sourceList = window.document.forms[0].parentList;
	for (var i = 0; i < sourceList.options.length; i++) {
		if (sourceList.options[i] != null)
		sourceList.options[i].selected = true;
	}
	return true;
}

// Deletes the selected items of supplied list.
function deleteSelectedItemsFromList (sourceList) {
	var maxCnt = sourceList.options.length;
	for (var i = maxCnt - 1; i >= 0; i--) {
		if ((sourceList.options[i] != null) && (sourceList.options[i].selected == true)) {
			sourceList.options[i] = null;
		}
   	}
}

// Bingo!!!
function addSrcToDestList (listname, id, name) {
	strList = 'window.document.forms[0].' + listname;
	destList = eval (strList);
	var len = destList.options.length;

	//Check if this value already exist in the destList or not
	//if not then add it otherwise do not add it.
	var found = false;
	for (var count = 0; count < len; count++) {
		if (destList.options[count] != null) {
			// alert (destList.options[count].value + ' ' + destList.options[count].text);
			if (id == destList.options[count].value) {
				found = true;
        	break;
      		}
		}
	}
	if (found != true) {
    	var oOption= new Option ("hello"); 
		destList.options[len] = oOption;
		destList.options[len].text = name;
		destList.options[len].value = id;
		destList.options[len].selected = false;
	}
}
</SCRIPT>
</HEAD>

<BODY>

<CENTER>
<FORM method=post>
<TABLE border=1 bgcolor="#ffffcc">
<TR>
<TD>
	<SELECT size=5 name=parentList multiple>
	</SELECT>
</TD>
</TR>
<TR>
<TD align=center>
	<INPUT type=button value="Add Item" onclick="javascript:small_window ('2.html');">
	<INPUT type=button value="Delete Item" onclick="javascript:deleteSelectedItemsFromList (parentList);">
</TD>
</TR>
</TABLE>
</FORM>
</CENTER>

</BODY>
</HTML>




<HTML>
<HEAD>
</HEAD>
<BODY>

<CENTER>
<TABLE bgcolor="#FFFFCC">
<TR>
	<TD><A Href="javascript:self.opener.addSrcToDestList ('parentList','15','Albania')">Albania</A></TD>
	<TD Align="center"></TD>
</TR>
<TR>
	<TD><A Href="javascript:self.opener.addSrcToDestList ('parentList','71','Algeria')">Algeria</A></TD>
	<TD Align="center"></TD>
</TR>
</TABLE>

</BODY>
</HTML>

Google