SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    266
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    When same options choose a second time,...

    When I chose same options a second time, it will not add to <select id="select4"> </select>
    What can I do? Thanks...
    Aynı optionu ikinci kez se&#231;tiğim zaman, <select id="select4"> </select>'e eklemeyecek.
    Ne yapabilirim? Teşekk&#252;rler...
    Code:
      
    <script type="text/javascript">
    
    
    function addOption (a) {
    
    var s = document.getElementById('select4');
    
    var val = [];
    var text = [];
    for(var i =0; i<s.options.length; i++)
    { val.push(s.options[i].value) ;  text.push(s.options[i].text) ;}
     // alert(val);
     // alert(text);
    
    if(s.options.length > 0) {
    for(var n = 0; n< s.options.length; n++) {
    if( s.options[n].value.indexOf(a.options[a.selectedIndex].value) == -1 && s.options[n].text.indexOf(a.options[a.selectedIndex].text) == -1  )
    { 
    s.options[s.options.length]= new Option(a.options[a.selectedIndex].value,a.options[a.selectedIndex].text) 
    }
    }
    }
    
    else { 
    
    s.options[s.options.length]= new Option(a.options[a.selectedIndex].value,a.options[a.selectedIndex].text) 
    
    }
    }
    
    </script>
    
    <select onchange="addOption(this)">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    </select>
    
    <select onchange="addOption(this)" >
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="30">30</option>
    <option value="40">40</option>
    </select>
    
    <select onchange="addOption(this)" >
    <option value="100">100</option>
    <option value="200">200</option>
    <option value="300">300</option>
    <option value="400">400</option>
    </select>
    
    <select id="select4">
    
    </select>
    Last edited by muazzez; Apr 12, 2007 at 12:13.

  2. #2
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The onchange event doesn't occur when you select the selected option - because that is not a "change". You can use the onclick event but it will occur every time the select is clicked - but that might be okay because you are checking that the same option doesn't get added twice.

  3. #3
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe
    Code:
    ...
    s.options.length=0;
    s.options[s.options.length]=new Option(a.options[a.selectedIndex].value,a.options[a.selectedIndex].text)
    }

  4. #4
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How about this.

    Here is a way 2 do it. Works so far. I hope it helps.
    HTML Code:
    <html>
    <body>
      
    <script language="JavaScript" type="text/javascript">
    
    
    
    function addOption (a) {
    	
    	if(a.selectedIndex){
    	
    	var s = window.document.looking.select4;
    	var outP=s.options.length;		<!--for developing proper index-->
    	var outP2=outP-1;			<!--array index starts at 0 while .length() returns starting w/1-->
    	var move=0;				
    	var val = [];
    	var text = [];
    	for(var i =0; i<s.options.length; i++)
    	{ val.push(s.options[i].value) ;  text.push(s.options[i].text) ;}
     	
    	
    	
    	if(!outP){
    	s.options[move]= new Option(a.options[a.selectedIndex].value,a.options[a.selectedIndex].text);<!--for first entry in array-->
    	}else{ 
    	s.options[outP]= new Option(a.options[a.selectedIndex].value,a.options[a.selectedIndex].text);
    	} 
    	
    	s.focus();		<!--focus on target select for allowing onchange event-->
    	a.selectedIndex=move;<!--allows for onchange event after each addOption.-->
    	}
    	else{
    	s.focus();
    	a.selectedIndex=move;
    	Alert("Please choose again.");<!-- to alert the user about the 0 value cannot be added had to add choose so the top options were not choosen to start.-->
    	
    }
    }
    </script>
    
    <form name="looking">
    
    <select onchange="addOption(this)">
    <option value="0">Choose</option>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    </select>
    
    <select onchange="addOption(this)" >
    <option value="0">Choose</option>
    <option value="10">10</option>
    <option value="20">20</option>
    <option value="30">30</option>
    <option value="40">40</option>
    </select>
    
    <select onchange="addOption(this)" >
    <option value="0">Choose</option>
    <option value="100">100</option>
    <option value="200">200</option>
    <option value="300">300</option>
    <option value="400">400</option>
    </select>
    
    <select id="select4">
    
    </select>
    </form>
    
    </html>     
     
    James
    New links coming soon....

  5. #5
    SitePoint Member
    Join Date
    Feb 2007
    Location
    Fresno, CA
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post When same options choose a second time,...

    I have kind of similar issue, when I try to assign the selectedindex.value to the selectedindex.text, the value get stuck to it and does not change when it is selected second time as I want the original selected.index.value to be passed on to the next function and the new selectedindex.value to be selected and passed on to the next page for sql command.

    I would really appreciate if you can select the following page and let me know what is wrong I am doing.

    Thanks,
    Ebaad.

    function setDynaList(arrDL){
    var oList1 = document.forms[arrDL[2]].elements[arrDL[1]];
    var oList2 = document.forms[arrDL[4]].elements[arrDL[3]];
    var arrList = arrDL[5];
    clearDynaList(oList2);

    if (oList1.selectedIndex == -1){
    oList1.selectedIndex = 0;
    }

    //alert(oList1.SelectedIndex.text + " " + oList1SelectedIndex.value);
    //var SelectedIndexValNew = oList.SelectedIndex.value;
    //SelectedIndex.value = SelectedIndex.text;
    var value = oList1[oList1.selectedIndex].value;
    var text = oList1[oList1.selectedIndex].text;
    var SINDEX = oList1.selectedIndex;
    var SITEM = oList1.selectedIndex.text;
    //oList1[oList1.selectedIndex].value = text;
    alert("VALUE: " + value + " TEXT: " + text + " SINDEX: " + SINDEX + " SITEM: " + SITEM);

    populateDynaList(oList2, value, arrList);

    return true;
    }


Bookmarks

Posting Permissions

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