Web Analytics Made Easy -
StatCounter Adding Non Duplicated Value into DropDownList - CodingForum

Announcement

Collapse
No announcement yet.

Adding Non Duplicated Value into DropDownList

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Adding Non Duplicated Value into DropDownList

    Hi, I have a parent page with a OpenPopup button, when it is clicked, it will popup a child window, user would be able to click on links on table, once clicked, the selected value would be add as new option in the parent's dropdownlist. How do I change the code so that it will check whether the dropdownlist has value that matched with the passed in value, if found exist, it will remove the existing value before adding the new passed in value?

    For example, we have 1,2,102456 in dropdownlist. If user selected 102456 in the child page, 102456 would be removed from dropdownlist and then 102456 would be added, so end up as 102456,1,2, the most recent entry would stay on top of list and selected.

    Please help!

    Source codes for both files :
    Parent.htm
    Code:
    <html>
    <head>
    <title>Untitled</title>
    <script language="JavaScript" type="text/javascript">
    <!--
    function openWin() { 
    var wintitle = "popup";
    var width = "707";
    var height = "350";
    LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
    windowproperties = 'height='+height+',width='+width+',top='+TopPosition;
    windowproperties +=',left='+LeftPosition+',scrollbars=no,resizable';
    dropdownSize2=document.TEST.slt.options.length;
    //-->if (dropdownSize2==dropdownSize){
    newwin = window.open('child.htm', wintitle, windowproperties); 
    //-->}
    }
    var values = new Array();
    var texts = new Array();
    var dropdownSize;
    var dropdownSize2;
    function getValues(){
    dropdownSize=document.TEST.slt.options.length;
    for (var i = 0; i < dropdownSize;i++){
    values[i]=document.TEST.slt.options[i].value;
    texts[i]=document.TEST.slt.options[i].text;
    }
    }
    
    function transferValue(tt){
    var transferredValue=tt;
    //-->var transferredValue=newwin.my.myname.value;
    document.TEST.slt.options[0] = new Option(transferredValue,transferredValue);
    for (var i = 1; i <= dropdownSize;i++){
    document.TEST.slt.options[i] = new Option(texts[i-1],values[i-1]);
    }
    document.TEST.slt.selectedIndex=0;
    document.TEST.slt.focus();
    }
    //-->
    </script>
    </head>
    <body onload="getValues()">
    <FORM name="TEST"> 
    <SELECT NAME="slt"> 
    <OPTION value="value0">1</OPTION> 
    <OPTION value="value1">2</OPTION> 
    <OPTION value="value2">102456</OPTION> 
    </SELECT><br><br>
    <input type="button" value="open popup" onclick="openWin()">
    </FORM> 
    </body>
    </html>

    child.htm
    Code:
    <html>
    <head>
    <title>Untitled</title>
    </head>
    <body onload="document.my.myname.focus()">
    <form name="my">
    <table border="1" width="100" id="table1">
         <tr>
              <td><a href="#" onclick="window.opener.getValues();window.opener.transferValue('102456');self.close();">102456</a></td>
         </tr>
         <tr>
              <td><a href="#" onclick="window.opener.getValues();window.opener.transferValue('202256');self.close();">202256</a></td>
         </tr>
         <tr>
              <td><a href="#" onclick="window.opener.getValues();window.opener.transferValue('302256');self.close();">302256</a></td>
         </tr>
    </table>
    <p>
    
    </p>
    </form>
    </body>
    </html>
Working...
X