Guest User

Untitled

a guest
Aug 8th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. <select name="mySelect" id="mySelect">
  2. <option value="1" id="option1">1</option>
  3. <option value="2" id="option2">2</option>
  4. </select>
  5.  
  6. var options = $$('select#mySelect option');
  7. var len = options.length;
  8. for (var i = 0; i < len; i++) {
  9. console.log('Option text = ' + options[i].text);
  10. console.log('Option value = ' + options[i].value);
  11. }
  12.  
  13. // replace 1 with index of an item you want to select
  14. options[1].selected = true;
  15.  
  16. $$('#mySelect option').find(function(ele){return !!ele.selected})
  17.  
  18. var selectThis = 'option1';
  19. $$('select#mySelectId option').each(function(o) {
  20. if(o.readAttribute('value') == selectThis) { // note, this compares strings
  21. o.selected = true;
  22. throw $break; // remove this if it's a multi-select
  23. }
  24. });
  25.  
  26. $('mySelect').setValue(1); // or whatever value you want to select
  27.  
  28. var myChoice = '2';
  29.  
  30. $$('select#mySelectId option').each(function(o) {
  31. o.selected = o.readAttribute('value') == myChoice;
  32. });
  33.  
  34. var itis = $(mySelectId).select('option[value="' + sValueToSelect + '"]');
  35. if ( itis && itis.length > 0 )
  36. itis[0].selected = true;
  37.  
  38. var selectThis = 'option1';
  39. $$('select#mySelect option').each(function(o){
  40. if(o.id==selectThis){o.selected = true;$break;}
  41. });
  42.  
  43. $('mySelect').value = 2; // 2 being the value you want selected
  44.  
  45. document.observe('dom:loaded', function() {
  46. $('mySelect').observe('change', function(event) {
  47. ...
  48. });
  49. });
Advertisement
Add Comment
Please, Sign In to add comment