Setting the values of a (multiple)select form element using javascript

[Hint: enter a letter and press the 'Select ALL matching Words' button]

Word Finder

The code

function set_matching_words(selectObj, txtObj) {
  var letter = txtObj.value;
 
  for(var i = 0; i < selectObj.length; i++) {
    if(selectObj.options[i].value.charAt(0) == letter) {
      selectObj.options[i].selected = true;
    } else {
      selectObj.options[i].selected = false;
    }
  }

}

Download the code here: javascript-form-field-example-3.zip

Back to article: set a form field using Javascript

  • Digg
  • del.icio.us
  • Netscape
  • Reddit
  • StumbleUpon
  • Technorati
  • YahooMyWeb

How to make a form without coding? Click here for a demo.

Follow me on twitter

HTML Forms
HTML Form Tutorial
The Form Tag
The Form Submit Button
Form design software
How to make a web form
Make web forms quickly, without coding

Javascript Form Handling
  Basics
Using getElementById to get the elements in a form
Set value of form element using JavaScript
Get value of form element using JavaScript
Handling multiple forms using JavaScript
JavaScript reset/clear a form
  Advanced
JavaScript Form Validation
Submitting a form using JavaScript
Can JavaScript Email a Form?
Switching the form action field dynamically
JavaScript Button

JavaScript Popup Windows
The window.open method
The window.close method
More ...

 
.   Copyright © 2003-2009 JavaScript-coder.com. All rights reserved.