Solved

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

Posted on 2015-02-06
3
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

630 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