• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
May 9, 2023May 9, 2023

How To Work With Conditional Radio Buttons in JavaScript

How To Work With Conditional Radio Buttons in JavaScript

Objective of this post

This post talks about how to create radio buttons in HTML and how to provide controls to them. The objective is after selecting one radio button one dropdown will appear but when you click on other ,some other dropdown will appear.

Create Radio Buttons in HTML

From W3 schools guideline the code for creating a radio buttons is

<input type="radio" name="prep" value="drop1"
onClick="getVal()">
select for Bus;<br>
<input type="radio" name="prep" value="drop2"
onClick="getVal()">
Select For Train;<br>

 

  • The objective to give same name is to make them mutually exclusive.
  • That means if I select one the other one will automatically get deselected.
  • Again the value is given to identify uniquely.
  • Now let us understand the the last parameter. onClick..This is given to understand what will happen if the corresponding radio got clicked.
  • Check is another property.Which says by default which radio will be checked. As per our requirements there should not be any radio in selected mode by default.

 

The selection or DropDown preparation:

To create dropdown we can give HTML syntax like-

<select id="bus" >
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>
<select id="train">
  <option value="volvo">Tufan</option>
  <option value="saab">Coal Field</option>
  <option value="opel">MMTS</option>
  <option value="audi">Local</option>
</select>

id is used to track these select as per our requirements.

The GetValue Function:

As per the requirements, if one of the radio gets clicked the corresponding dropdown should show.To activate this feature we need to design the getValue function.It is defined in the below written manner.

function getVal(){
var radios = document.getElementsByName('prep');
//This step will get all the radio whose name is prep.
for (var i = 0, length = radios.length; i < length; i++) {
//this is to loop through the radio to determine which one is selected 
 if (radios[i].checked) {
        if(radios[i].value=="drop1")
  {
//if the first one is selected that means user is looking for Bus service 
 document.getElementById("bus").style.display="block";
  document.getElementById("train").style.display="none"
        }
       if(radios[i].value=="drop2")
  {
 //if the Second one is selected that means user is looking for Train service
 document.getElementById("train").style.display="block";
  document.getElementById("bus").style.display="none"
        }

}
}
}

 

Syntax to visible or hide for a selectbox:

The syntax we will use to display or hide the selectbox..

//none for hide
document.getElementById("bus").style.display="none"
document.getElementById("train").style.display="none"
//block for visible
document.getElementById("bus").style.display="block"
document.getElementById("train").style.display="block"

How to handle these?? Well to start with, in the body tag, we will call a function that will make both the select box hide.

<BODY bgColor="#FFFFFF" onLoad="hideDiv()">

and the function will be-

function hideDiv( ) {

document.getElementById(“bus”).style.display=”none”
document.getElementById(“train”).style.display=”none”

}

Real Implementation

Here is the total code to work with Radiobutton.

<HTML>
<HEAD>
<TITLE>Sample Form with Radio Buttons</TITLE>
<SCRIPT Language="JavaScript">
<!--

function hideDiv( ) {

document.getElementById(“bus”).style.display=”none”
document.getElementById(“train”).style.display=”none”

}
function getVal(){

var radios = document.getElementsByName(‘prep’);

for (var i = 0, length = radios.length; i < length; i++ ) {
if (radios[i].checked) {

if(radios[i].value==”drop1″)
{
document.getElementById(“bus”).style.display=”block”;
document.getElementById(“train”).style.display=”none”
}
if(radios[i].value==”drop2″)
{
document.getElementById(“train”).style.display=”block”;
document.getElementById(“bus”).style.display=”none”
}

}

}
}
//->
</SCRIPT>
</HEAD>
<BODY bgColor=”#FFFFFF” onLoad=”hideDiv()”>
<form name=”form1″>
<p>Select one option for Travel</p>
<blockquote>
<p>
<input type=”radio” name=”prep” value=”drop1″ onClick=”getVal()”>
select for Bus;<br>
<input type=”radio” name=”prep” value=”drop2″ onClick=”getVal()”>
Select For Train;<br>
</p>
<select id=”bus” >
<option value=”volvo”>Volvo</option>
<option value=”saab”>Saab</option>
<option value=”opel”>Opel</option>
<option value=”audi”>Audi</option>
</select>
<select id=”train”>
<option value=”volvo”>Tufan</option>
<option value=”saab”>Coal Field</option>
<option value=”opel”>MMTS</option>
<option value=”audi”>Local</option>
</select>
</blockquote>
</form>
</BODY>
</HTML>

 

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Zoom Meeting Outfits: 4 Comfortable Yet Dressy Tops to Wear on Your Next Video Call
  • Zoom Clone: 6 Significant Benefits To Lure You In Launching
  • Your Shopify Store – Top 5 Essential Inventory Management Apps
  • All-inclusive You Need To Know About Skirting Beyond 2020
  • Yohuton Health and Fitness Smartwatch for Men Beyond 2020

Categories

  • Adventure
  • Agile Methodology
  • AI
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Companies
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Earn Money Online
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Graphic Designers
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Magento
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Office Productivity
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • RDT
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOA
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VBscript
  • VPN
  • Web Development
  • WebHosting
  • WebServices
  • Wildlife
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948