Solved

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

Posted on 2015-02-06
3
90 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

757 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now