Change select list selected value

Discussion in 'JavaScript' started by greatlogix, Apr 21, 2011.

  1. #1
    I have this select list on a page
    <select name="otype[]" class="txtText" id="otype[]">
    			  <option value="Processing">Processing</option>
    			  <option value="Shipped">Shipped</option>
                                        </select>
    
    HTML:
    Scripts generates 5 select lists on the page. I need to change all select lists options to "Shipped" if customer clicks on "Shipped" on top of the page and "Processing" if customer clicks on "Processing" link (<a href="javascript:select_processing()">Processing</a>)

    Can somebody help me in doing so?
     
    greatlogix, Apr 21, 2011 IP
  2. x319

    x319 Well-Known Member

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    103
    #2
    
    function selectOType(otype) {
        document.getElementById('otype[]').value = otype;
    }
    // <a href="javascript:selectOType('Processing');return 0;">Processing</a>
    // <a href="javascript:selectOType('Shipped');return 0;">Shipped</a>
    
    PHP:
    Maybe be some errors, just did that from mind.
     
    x319, Apr 21, 2011 IP
  3. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #3
    I think i did not explain well... I have code like this

    
    <form>
    <select name="otype[]" class="txtText" id="otype[]">
                  <option value="Processing">Processing</option>
                  <option value="Shipped">Shipped</option>
                                        </select>
    
    some code......
    
    <select name="otype[]" class="txtText" id="otype[]">
                  <option value="Processing">Processing</option>
                  <option value="Shipped">Shipped</option>
                                        </select>
    
    some code.......
    
    <select name="otype[]" class="txtText" id="otype[]">
                  <option value="Processing">Processing</option>
                  <option value="Shipped">Shipped</option>
                                        </select>
    
    
    HTML:
    I just want to change selected value for all select lists with one mouse click..... how to....?
     
    greatlogix, Apr 21, 2011 IP
  4. x319

    x319 Well-Known Member

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Yup, that should do it.
     
    x319, Apr 21, 2011 IP
  5. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #5
    nah.. that doesn't work
     
    greatlogix, Apr 21, 2011 IP
  6. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #6
    can anybody else help me on this topic...?
     
    greatlogix, May 9, 2011 IP
  7. Jan Novak

    Jan Novak Peon

    Messages:
    121
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    0
    #7
    One line is enough to do it ;)

    
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=windows-1250">
      <meta name="generator" content="PSPad editor, www.pspad.com">
      <title></title>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
      
      <script>
            function select_processing(){
              jQuery(".txtText option[value=Shipped]").attr('selected', 'selected');
            }
      </script>
      
      </head>
      <body>
              <select name="otype[]" class="txtText" id="otype[]">
                  <option value="Processing">Processing</option>
                  <option value="Shipped">Shipped</option>
                                        </select>
    
    some code......
    
    <select name="otype[]" class="txtText" id="otype[]">
                  <option value="Processing">Processing</option>
                  <option value="Shipped">Shipped</option>
                                        </select>
    
    some code.......
    
    <select name="otype[]" class="txtText" id="otype[]">
                  <option value="Processing">Processing</option>
                  <option value="Shipped">Shipped</option>
                                        </select>
                                        
    <a href="javascript:select_processing()">set to shipped</a>                                    
      </body>
    </html>
    
    Code (markup):
     
    Jan Novak, May 9, 2011 IP
    greatlogix likes this.
  8. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #8
    Thanks Jan Novak for a Perfect solution. Green for u.
     
    greatlogix, May 10, 2011 IP