Results 1 to 3 of 3

Thread: VBScript-How To Pass Checked Checkbox Value To Listbox; Remove ListItem From List Box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    101

    Question VBScript-How To Pass Checked Checkbox Value To Listbox; Remove ListItem From List Box

    Good day !

    Let say I got 2 buttons, called cmdAdd and cmdRemove. When the user click on cmdAdd, the script should automatically add in all the value of checked checkbox (let say chkData) to the list (let say lstItem).

    In another case, when the user select items in the list and click on cmdRemove, the selected shall be removed from the list and the check box of the selected value shall be unchecked.

    How can I accomplish this using VBScript ?

    I try to search through the webs but I can hardly find any references for me to refer, even in MSDN.

    Besides that, I choose VBScript cauz I more familiar with VB compare to JavaScript.

    Help. Really need help.

    Thanks a lot !

    SonicWave

  2. #2
    Frenzied Member andreys's Avatar
    Join Date
    Sep 2002
    Location
    Los Angeles
    Posts
    1,615
    Here is example with JavaScript:

    Code:
    <html>
    <head>
    <title>New Page 1</title>
    </head>
    <script language="JavaScript">
    function listitems(strItem, boolItem){
    var found_item=false;
    	if(boolItem){
    		for(i=0;i<D1.options.length;i++){
    			if(D1.options[i].value==strItem){
    				found_item=true;
    				break;
    				}
    			else{
    				found_item=false;
    				}
    			}
    		if(!found_item){
    			var opt=document.createElement("OPTION");
    			opt.value=strItem;
    			opt.text=strItem;
    			D1.add(opt);
    			}
    		}
    	else{
    		for(i=0;i<D1.options.length;i++){
    			if(D1.options[i].value==strItem){
    				D1.remove (i);
    				}
    			}
    		}
    	}
    
    function removefromlist(){
    	var iIndex=D1.selectedIndex;
    	if(iIndex==-1){
    		alert("Please select item(s) you wish to remove!")
    		}
    	else{
    		for(y=0;y<C1.length;y++){
    			for(i=0;i<D1.options.length;i++){
    				if(C1[y].value==D1.options[i].value && C1[y].checked && D1.options[i].selected){
    					C1[y].checked=false;
    					D1.remove(i);
    				}
    			}
    		}
    	}
    }
    </script>
    <body>
    <p><select size="7" name="D1" multiple>
    </select></p>
    <p>
    <input type="checkbox" name="C1" value="Item 1" onclick="listitems(this.value,this.checked)">
    <input type="checkbox" name="C1" value="Item 2" onclick="listitems(this.value,this.checked)">
    <input type="checkbox" name="C1" value="Item 3" onclick="listitems(this.value,this.checked)">
    <br>
    <input type="button" value="remove" name="remove" onclick="removefromlist()"
    </p>
    </body>
    </html>

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    101

    Red face Thanks.

    Good day.

    Thanks for the advice and code samples.

    I have tried write my own and I change another way round how to implement this.

    I made it like this: the user click on the checkbox and it will add the checked item into the list box. Else if the user unchecked it, it will be removed from the list.

    I implement it using JavaScript as well. I have refered to a nice book - Pure Java Script, from SAMS.

    Below is the sample codes I have written. It may be helpful for anyone who need it.

    Thanks again.

    <HTML>

    <HEAD>
    <TITLE>Tester Page</TITLE>
    </HEAD>

    <BODY>
    <script language="JavaScript">
    function check1Clicked(checkboxName, listName) {
    if (checkboxName.checked) {
    oChild = new Option(checkboxName.value,checkboxName.value);
    listName.add (oChild) ;
    } else {
    for (var i=0; i < listName.length; i++) {
    if (listName[i].value ==checkboxName.value) {
    listName.remove(i);
    }
    }
    }
    }
    </script>
    <form name="form1" method="post" action="">
    <TABLE BORDER>
    <TR><TD>
    <INPUT TYPE="CHECKBOX" NAME="TChk1" onClick="check1Clicked(TChk1,lst)" value="haha1">
    <INPUT TYPE="CHECKBOX" NAME="TChk2" onClick="check1Clicked(TChk2,lst)" value="haha2">
    <INPUT TYPE="CHECKBOX" NAME="TChk3" onClick="check1Clicked(TChk3,lst)" value="haha3">
    <INPUT TYPE="CHECKBOX" NAME="TChk4" onClick="check1Clicked(TChk4,lst)" value="haha4">
    </TD></TR>
    </TABLE>

    <p>
    <select name="lst" size="2" multiple id="lst">
    </select>
    </p>
    </form>
    </BODY>
    </HTML>

    SonicWave
    Last edited by SonicWave; Jan 24th, 2003 at 12:44 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width