form.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * $Id: TinyMCE_DOMUtils.class.js 91 2006-10-02 14:53:22Z spocke $
  3. *
  4. * @author Moxiecode
  5. * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.
  6. */
  7. mox.require([
  8. 'mox.DOM'
  9. ], function() {
  10. // Shorten class names
  11. var DOM = mox.DOM;
  12. /**#@+
  13. * @class This class contains various form utility methods.
  14. * @member mox.dom.Form
  15. * @static
  16. */
  17. mox.create('static mox.dom.Form', {
  18. /**#@+
  19. * @method
  20. */
  21. /**
  22. * Selects a option element inside a select list by value.
  23. *
  24. * @param {Form} f Form HTML element containing the select element.
  25. * @param {string} n Name of select element to find option in.
  26. * @param {string} v Option value to look for.
  27. * @return {Element} Option HTML element if it was found or null if it wasn't.
  28. */
  29. selectOption : function(f, n, v) {
  30. var i, ol = f.elements[n].options;
  31. for (i=0; i<ol.length; i++) {
  32. if (ol[i].value == v) {
  33. ol[i].selected = true;
  34. return ol[i];
  35. }
  36. }
  37. return null;
  38. },
  39. /**
  40. * Add a new option element to a select element.
  41. *
  42. * @param {Form} f Form HTML element containing the select element.
  43. * @param {string} n Name of select element to option to.
  44. * @param {string} on Option name/title to add.
  45. * @param {string} on Option value to add.
  46. * @return {Element} Option HTML element that was added.
  47. */
  48. addOption : function(f, n, on, ov) {
  49. var e = f.elements[n], ol = e.options;
  50. o.selected = true;
  51. return ol.push(new Option(on, ov));
  52. },
  53. getRadioValue : function(f, n) {
  54. var i, nl;
  55. for (i=0, nl = f.elements[n]; i<nl.length; i++) {
  56. if (nl[i].checked)
  57. return nl[i].value;
  58. }
  59. return null;
  60. },
  61. /**
  62. * Returns a form item value this will also handle select
  63. * elements/checkboxes and radio buttons.
  64. *
  65. * @param {Form} f Form element name to check item value in.
  66. * @param {string} n Name of form item to get value from.
  67. * @return {string} Form item value or null if it wasn't found.
  68. */
  69. getValue : function(f, n) {
  70. var e = f.elements[n];
  71. switch (e.type) {
  72. case 'SELECT':
  73. return e.options[e.selectedIndex].value;
  74. }
  75. return e.value;
  76. }
  77. /**#@-*/
  78. });
  79. });