Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add selectOptionByValue

  • Loading branch information...
commit 646b20d08a893385e693c593fa82c58485e6933f 1 parent cfee969
kangax authored
Showing with 30 additions and 0 deletions.
  1. +17 −0 form.element.methods.js
  2. +13 −0 test/form.element.methods.html
View
17 form.element.methods.js
@@ -28,4 +28,21 @@ Field.Methods.present = function(element) {
(/select-one|select-multiple/.test(t) && element.selectedIndex != -1));
};
+/**
+ *
+ *
+ **/
+Field.Methods.selectOptionByValue = function(element, value) {
+ var index = 0;
+ element = $(element);
+ Element.childElements(element).each(function(element, i) {
+ if (element.value == value) {
+ index = i;
+ throw $break;
+ }
+ })
+ element.selectedIndex = index;
+ return element;
+};
+
Element.addMethods();
View
13 test/form.element.methods.html
@@ -17,6 +17,11 @@
<div id="testlog"></div>
<input type="text" name="dummy_input" value="" id="dummy_input" />
+ <select id="dummy_select">
+ <option value="foo" selected="selected">foo</option>
+ <option value="bar">bar</option>
+ <option value="baz">baz</option>
+ </select>
<script type="text/javascript">
new Test.Unit.Runner({
@@ -25,6 +30,14 @@
this.assert(input.isBlank());
input.setValue('foo');
this.assert(!input.isBlank());
+ },
+ testFieldMethodsSelectOptionByValue: function() {
+ var element = $('dummy_select');
+
+ element.selectOptionByValue('bar');
+ this.assert(element.selectedIndex == 1);
+ element.selectOptionByValue('baz');
+ this.assert(element.selectedIndex == 2);
}
})
</script>
Please sign in to comment.
Something went wrong with that request. Please try again.