Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. JavaScript
  4. select with same data/options

select with same data/options

Scheduled Pinned Locked Moved JavaScript
javascriptalgorithmsdata-structureshelp
8 Posts 3 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Morgs Morgan
    wrote on last edited by
    #1

    hi guys, i have six(6) select controls all with values 1-6. when i click submit in javascript i would like to check that a select's value has not already been selected by the other five (5) selects. can anyone please help me how i can do this, i have tried logic after logic and yet still no joy at all...please help me. i tried:

        //check template orders
        var ordercntr = false;//just a control key
        var partial = '';//i'm appending the selects ids to this variable on each iteration.
        var orderfinal = new Array();//will add the successful values of my selects(inside my if statement)
        for (l = 0; l < order.length; l++) {
            //looping through an array that contains selects ids(order)
           //select ids have the form: sel\_0, sel\_1, sel\_2, sel\_3, sel\_4, sel\_5 (there six selects in total)
            partial += order\[l\] + '~';//appending to my partial variable, the current select's id
            var d = document.getElementById(order\[l\]).value;//getting the value of the select
            if (partial.search(d) < 1)
               {
                 //i'm searching for whether the selected value e.g. 1 has already been appended to my
                 //partial variable 'partial' which at first iteration has a value of sel\_0~, second iteration=sel\_0~sel\_1
                  orderfinal\[l\] = d; ordercntr = true;
               }
            else
               {
                  ordercntr = false; break;
               }
        }
    

    The logic works like a charm if the selected values in the selects are in the order 1-6 But fails badly if lets say the second(2) select has the value 5(or something else other than 2) i know i have to revisit the entire logic but i have tried and now i'm asking for help before i try forever. I will appreciate any help. Morgs

    L S 2 Replies Last reply
    0
    • M Morgs Morgan

      hi guys, i have six(6) select controls all with values 1-6. when i click submit in javascript i would like to check that a select's value has not already been selected by the other five (5) selects. can anyone please help me how i can do this, i have tried logic after logic and yet still no joy at all...please help me. i tried:

          //check template orders
          var ordercntr = false;//just a control key
          var partial = '';//i'm appending the selects ids to this variable on each iteration.
          var orderfinal = new Array();//will add the successful values of my selects(inside my if statement)
          for (l = 0; l < order.length; l++) {
              //looping through an array that contains selects ids(order)
             //select ids have the form: sel\_0, sel\_1, sel\_2, sel\_3, sel\_4, sel\_5 (there six selects in total)
              partial += order\[l\] + '~';//appending to my partial variable, the current select's id
              var d = document.getElementById(order\[l\]).value;//getting the value of the select
              if (partial.search(d) < 1)
                 {
                   //i'm searching for whether the selected value e.g. 1 has already been appended to my
                   //partial variable 'partial' which at first iteration has a value of sel\_0~, second iteration=sel\_0~sel\_1
                    orderfinal\[l\] = d; ordercntr = true;
                 }
              else
                 {
                    ordercntr = false; break;
                 }
          }
      

      The logic works like a charm if the selected values in the selects are in the order 1-6 But fails badly if lets say the second(2) select has the value 5(or something else other than 2) i know i have to revisit the entire logic but i have tried and now i'm asking for help before i try forever. I will appreciate any help. Morgs

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      there are several ways to do this. here are a few: 1. compare 2 with 1; then 3 with 1 and 2; then... 2. create a bit mask where each bit corresponds to a value (bit1=value 1, bit2=value 2, bit3= value 3,...); start at zero, then OR with the bit value, in pseudo-code bitmask=bitmask | (1< and finally check the final value is what it should be: 0x7E 3. similar to #2, use a bit mask; before ORing in a new bit, check the bit is still zero; if it isn't, there is a duplicate. 4. same as #2 or #3, however use a string instead of a bit mask. 5. same as #2, however use an array, have it sorted, then compare with what it should be. :) Luc Pattyn [[Forum Guidelines]](/KB/scrapbook/ForumGuidelines.aspx) [[My Articles]](http://www.perceler.com/articles1.php) Nil Volentibus Arduum Please use [**<PRE>**](/Tips/42071/Use-PRE-tags.aspx) tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      M 1 Reply Last reply
      0
      • L Luc Pattyn

        there are several ways to do this. here are a few: 1. compare 2 with 1; then 3 with 1 and 2; then... 2. create a bit mask where each bit corresponds to a value (bit1=value 1, bit2=value 2, bit3= value 3,...); start at zero, then OR with the bit value, in pseudo-code bitmask=bitmask | (1< and finally check the final value is what it should be: 0x7E 3. similar to #2, use a bit mask; before ORing in a new bit, check the bit is still zero; if it isn't, there is a duplicate. 4. same as #2 or #3, however use a string instead of a bit mask. 5. same as #2, however use an array, have it sorted, then compare with what it should be. :) Luc Pattyn [[Forum Guidelines]](/KB/scrapbook/ForumGuidelines.aspx) [[My Articles]](http://www.perceler.com/articles1.php) Nil Volentibus Arduum Please use [**<PRE>**](/Tips/42071/Use-PRE-tags.aspx) tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        M Offline
        M Offline
        Morgs Morgan
        wrote on last edited by
        #3

        Hi Luc, Thanks for your suggestion, i found dodgy solution though. 1. I put all my selects ids in an array (e.g. ItemOrder) 2. i loop through the ItemOrder likse so;;

        var count=0;
        var isMoreThanTwo=false;
        for(i=0;i1 then user chose the current select's value in more than one select
        //otherwise count will always remain 1 if this select's value is unique
        if(count>1){isMoreThanTwo=true;break;}

           }
           if(isMoreThanTwo)break;
        

        }
        if(count<=1)
        {
        //user chose a unique value for each select
        }
        else
        {
        //error, user has selected duplicate values in the selects!
        }

        The above works like a charm, might not be the best solution but does just the trick Thanks, Morgs

        L 1 Reply Last reply
        0
        • M Morgs Morgan

          Hi Luc, Thanks for your suggestion, i found dodgy solution though. 1. I put all my selects ids in an array (e.g. ItemOrder) 2. i loop through the ItemOrder likse so;;

          var count=0;
          var isMoreThanTwo=false;
          for(i=0;i1 then user chose the current select's value in more than one select
          //otherwise count will always remain 1 if this select's value is unique
          if(count>1){isMoreThanTwo=true;break;}

             }
             if(isMoreThanTwo)break;
          

          }
          if(count<=1)
          {
          //user chose a unique value for each select
          }
          else
          {
          //error, user has selected duplicate values in the selects!
          }

          The above works like a charm, might not be the best solution but does just the trick Thanks, Morgs

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          that looks OK, except if(d==document.getElementById(ItemOrder[i]).value) should use a, not i. Keeping the approach, the code can be further improved by: - reducing the inner loop to for(a=i+1;a<ItemOrder.length;a++); - dropping the count variable - renaming the isMoreThanTwo variable to thereAreDuplicates something along these lines:

          var thereAreDuplicates=false;
          for(i=0;i<ItemOrder.length;i++) {
          var d=document.getElementById(ItemOrder[i]).value;
          for(a=i+1;a<ItemOrder.length;a++) {
          if(d==document.getElementById(ItemOrder[a]).value) thereAreDuplicates=true;
          }
          if(thereAreDuplicates)break;
          }

          :) .

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          M 1 Reply Last reply
          0
          • L Luc Pattyn

            that looks OK, except if(d==document.getElementById(ItemOrder[i]).value) should use a, not i. Keeping the approach, the code can be further improved by: - reducing the inner loop to for(a=i+1;a<ItemOrder.length;a++); - dropping the count variable - renaming the isMoreThanTwo variable to thereAreDuplicates something along these lines:

            var thereAreDuplicates=false;
            for(i=0;i<ItemOrder.length;i++) {
            var d=document.getElementById(ItemOrder[i]).value;
            for(a=i+1;a<ItemOrder.length;a++) {
            if(d==document.getElementById(ItemOrder[a]).value) thereAreDuplicates=true;
            }
            if(thereAreDuplicates)break;
            }

            :) .

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            M Offline
            M Offline
            Morgs Morgan
            wrote on last edited by
            #5

            Thanks Luc, that looks pretty aswell, Oops your first comment was just a typo when i was writing at the forum otherwise i have it correct in my code.

            1 Reply Last reply
            0
            • M Morgs Morgan

              hi guys, i have six(6) select controls all with values 1-6. when i click submit in javascript i would like to check that a select's value has not already been selected by the other five (5) selects. can anyone please help me how i can do this, i have tried logic after logic and yet still no joy at all...please help me. i tried:

                  //check template orders
                  var ordercntr = false;//just a control key
                  var partial = '';//i'm appending the selects ids to this variable on each iteration.
                  var orderfinal = new Array();//will add the successful values of my selects(inside my if statement)
                  for (l = 0; l < order.length; l++) {
                      //looping through an array that contains selects ids(order)
                     //select ids have the form: sel\_0, sel\_1, sel\_2, sel\_3, sel\_4, sel\_5 (there six selects in total)
                      partial += order\[l\] + '~';//appending to my partial variable, the current select's id
                      var d = document.getElementById(order\[l\]).value;//getting the value of the select
                      if (partial.search(d) < 1)
                         {
                           //i'm searching for whether the selected value e.g. 1 has already been appended to my
                           //partial variable 'partial' which at first iteration has a value of sel\_0~, second iteration=sel\_0~sel\_1
                            orderfinal\[l\] = d; ordercntr = true;
                         }
                      else
                         {
                            ordercntr = false; break;
                         }
                  }
              

              The logic works like a charm if the selected values in the selects are in the order 1-6 But fails badly if lets say the second(2) select has the value 5(or something else other than 2) i know i have to revisit the entire logic but i have tried and now i'm asking for help before i try forever. I will appreciate any help. Morgs

              S Offline
              S Offline
              sydongda
              wrote on last edited by
              #6

              just sum six(6) select controls , and compare with (1 + 6) * 6 / 2. equals means ok

              L 1 Reply Last reply
              0
              • S sydongda

                just sum six(6) select controls , and compare with (1 + 6) * 6 / 2. equals means ok

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                {1,1,1,6,6,6} doesn't contain duplicates? :confused:

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                S 1 Reply Last reply
                0
                • L Luc Pattyn

                  {1,1,1,6,6,6} doesn't contain duplicates? :confused:

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  S Offline
                  S Offline
                  sydongda
                  wrote on last edited by
                  #8

                  :-O

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups