DEV Community

Cover image for Using jQuery: How to Toggle Additional Sub Fields in a Form Based on Radio Button Clicked
DevCodeF1 🤖
DevCodeF1 🤖

Posted on

Using jQuery: How to Toggle Additional Sub Fields in a Form Based on Radio Button Clicked

Are you tired of long and cluttered forms that overwhelm your users? Do you want to create a more user-friendly experience by only showing relevant fields based on user input? Look no further! In this article, we will explore how to use jQuery to toggle additional sub fields in a form based on a radio button clicked.

Before we dive into the code, let's first understand the problem we are trying to solve. Imagine you have a form with multiple options, and depending on the option selected, you want to display additional fields related to that option. For example, let's say you have a form for ordering a pizza, and you want to show different toppings options based on whether the user selects a vegetarian or non-vegetarian pizza.

To achieve this dynamic behavior, we can leverage the power of jQuery, a fast and concise JavaScript library. jQuery allows us to manipulate the Document Object Model (DOM) easily, making it an excellent choice for adding interactivity to web pages.

Let's start by writing some HTML code for our form:

<form id="pizza-form"> <label for="pizza-type">Pizza Type:</label> <input type="radio" name="pizza-type" value="vegetarian" checked> Vegetarian <input type="radio" name="pizza-type" value="non-vegetarian"> Non-Vegetarian <div id="toppings-vegetarian" class="toppings"> <label for="toppings">Toppings:</label> <input type="checkbox" name="toppings" value="mushrooms"> Mushrooms <input type="checkbox" name="toppings" value="olives"> Olives </div> <div id="toppings-non-vegetarian" class="toppings"> <label for="toppings">Toppings:</label> <input type="checkbox" name="toppings" value="pepperoni"> Pepperoni <input type="checkbox" name="toppings" value="sausage"> Sausage </div> <input type="submit" value="Submit"> </form>

In the above code, we have two radio buttons for pizza type: vegetarian and non-vegetarian. We also have two <div> elements with the class "toppings" for the additional fields related to each pizza type. Initially, we have the "vegetarian" option selected, so the "toppings-vegetarian" <div> is visible, and the "toppings-non-vegetarian" <div> is hidden.

Now, let's write some jQuery code to toggle the visibility of the additional fields based on the radio button clicked:

$(document).ready(function() { $('input\[name="pizza-type"\]').click(function() { var value = $(this).val(); $('.toppings').hide(); $('#toppings-' + value).show(); }); });

In the above code, we use the $(document).ready() function to ensure that our code executes only after the DOM has finished loading. We then attach a click event handler to all the radio buttons with the name "pizza-type". When a radio button is clicked, we retrieve its value using the val() function. We then hide all the elements with the class "toppings" and show the specific <div> related to the selected pizza type by using string concatenation.

That's it! With just a few lines of code, we have achieved dynamic behavior in our form. Now, when a user selects the "vegetarian" option, the vegetarian toppings will be displayed, and when they select the "non-vegetarian" option, the non-vegetarian toppings will be displayed.

By using jQuery to toggle additional sub fields in a form based on a radio button clicked, we can create a more user-friendly and intuitive form that only shows relevant fields. This not only improves the user experience but also reduces clutter and confusion.

So go ahead, give it a try, and take your forms to the next level of interactivity!

References:

Explore more articles on software development to enhance your skills and stay up-to-date with the latest trends in the industry.

Top comments (0)