Laravel-9 Tutorial – Laravel 9 jQuery Show Hide Div with Radio Button Tutorial

In this tutorial, we will learn how to hide and show an HTML div element using the radio button in the Laravel 9 application with the help of jQuery.

Laravel is a prevalent PHP web application framework for developing modern, robust, scalable web applications. jQuery, on the other side, is a profound JavaScript library that streamlines client-side scripting of HTML.

Radio buttons are a more significant part of the form. It is a type of graphical user interface (GUI) element used in software and web application development. It is generically used to show a list of options from which a user can select only one option.

Before we create a show hide div feature using jQuery in Laravel, we must understand a bit more about radio buttons.

Radio buttons contain a tiny circle called the radio button and a label that defines the option. When a site visitor clicks on a radio button, it transforms its state to selected, and all other radio buttons in the group become deselected.

How to Show Hide Div in Laravel 9 using Radio Button and jQuery

  • Step 1: Install Laravel Framework
  • Step 2: Build Controller File
  • Step 3: Register New Route
  • Step 4: Create Blade View
  • Step 5: Test Laravel Application

Install Laravel Framework

To install the laravel framework on your development system, you have to consider the following requirements:

  • PHP >= 7.3
  • Composer (Dependency Manager for PHP)
  • MySQL or any other relational database

Next, you have to install the Composer on your system; if you still need to do it, you can download and install it from the official website:

Then, open your terminal or command prompt and execute the following command to install Laravel:

composer create-project laravel/laravel --prefer-dist laravel-blog
cd laravel-blog

Build Controller File

Open your command line interface and navigate to your Laravel project directory.

Use the following command to create a new controller:

php artisan make:controller FormController

You have to update the given code into the app/Http/Controllers/FormController.php file.

namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateSupportFacadesBlade;
class FormController extends Controller
    public function index()
        return view('home');

Register New Route

Routes in Laravel help you create the URLs that your app will respond to; hence you have to open and add the given code into the routes/web.php file.

use IlluminateSupportFacadesRoute;
use AppHttpControllersFormController;

Route::get('/home', [FormController::class, 'index']);

Create Blade View

The blade is the default templating engine in Laravel; it offers a robust syntax for creating views in laravel.

Blade view file takes the simple, readable PHP code and quickly generates HTML code.

Put following code within the resources/views/home.blade.php file.

<!DOCTYPE html>
    <title>Laravel Tut</title>
    <link href="" rel="stylesheet">
    <link rel="stylesheet" href=""> 
    <script src="//"></script>
<div class="container mt-5">
      <h2 class="mb-3">Laravel jQuery Radio Button Div Hide Show Example</h2>
      <div class="mb-4">
        <div><input type="radio" name="type" value="userProfile" /> User Profile</div>
        <div><input type="radio" name="type" value="products" /> Products</div>
      <div class="userProfile radioBtnChoose mb-3">
        <label>User Profile:</label>
        <input type="text" placeholder="Search user" />
      <div class="products radioBtnChoose">
        <input type="text" placeholder="Search product" />
        $(document).ready(function () {
          $('input[type="radio"]').click(function () {
            var inputValue = $(this).attr("value");
            var target = $("." + inputValue);

Test Laravel Application

We are going to start the Laravel development server:

Hence, run the following command to start the Laravel development server:

php artisan serve

Open your app using the following URL:

Laravel 9 jQuery Show Hide Div with Radio Button Tutorial


Radio buttons are frequently used in various forms, surveys, and other applications where the requirement is to make a single choice from the given options.

Radio buttons are extremely facile and relatively easy to use, and their visual design makes it clear that only one option can be selected at a time.

In this guide, we learned How to show and hide div components based on the selected state of radio buttons in Laravel using the jQuery library.

Leave a Reply

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