Solved

Automatically check box when certain item is selected in a select drop down

Posted on 2015-02-06
3
92 Views
Last Modified: 2015-02-06
I have a SELECT and a CHECKBOX control on my form. When a certain option is selected (Volvo), I'd like the checkbox to automatically get checked. Can someone assist?

<select>
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
</select>
<INPUT TYPE=CHECKBOX NAME="volvo">Yes! Volvo was selected!<P>
0
Comment
Question by:earwig75
  • 2
3 Comments
 
LVL 7

Accepted Solution

by:
Robert Sherman earned 500 total points
ID: 40594694
Quick and sloppy version:

<select id="ddlCar" onchange="var e = document.getElementById('ddlCar');var strUser = e.options[e.selectedIndex].text;var bool_volvo = (strUser == 'Volvo');document.getElementById('check_volvo').checked = bool_volvo;">
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
</select>
<INPUT TYPE=CHECKBOX NAME="volvo" id="check_volvo">Yes! Volvo was selected!<P> 
</body>
</html>

Open in new window

0
 
LVL 7

Expert Comment

by:Robert Sherman
ID: 40594719
Here's the same thing, with code separated out and a little easier to follow:

<script>
  function volvoCheck() {
  var e = document.getElementById('ddlCar');
  var strUser = e.options[e.selectedIndex].text;
  if (strUser == 'Volvo')  document.getElementById('check_volvo').checked = true;
}
</script>
<select id="ddlCar" onchange="volvoCheck();">
   <option value="">Please select:</option>
   <option value="volvo">Volvo</option>
   <option value="saab">Saab</option>
</select>
<INPUT TYPE=CHECKBOX NAME="volvo" id="check_volvo">Yes! Volvo was selected!<P> 
</body>
</html>

Open in new window

0
 

Author Closing Comment

by:earwig75
ID: 40594725
Thank you!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Converting dates in JavaScript 2 35
add a 3rd selection to sum 7 29
Is there a way to use PhantomJS on a shared server? 4 17
Button and js nou working 3 7
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to position overlapping items using z-index in CSS. They will also learn the restrictions on the z-index property.  Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Red.…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question