Share this:

In web development, creating dynamic and interactive user interfaces is essential for a seamless user experience. One common requirement is to show or hide certain sections of a webpage based on user input. In this tutorial, we will explore how to achieve this functionality using Laravel, a popular PHP framework, and jQuery, a powerful JavaScript library.

Specifically, we will focus on using radio buttons to control the visibility of div elements on a webpage.

Prerequisites:

Before proceeding with this tutorial, you should have a basic understanding of Laravel, PHP, and jQuery. Familiarity with HTML and CSS will also be beneficial.

Ensure that you have a Laravel project set up and running on your local development environment.

Step 1: Setting up the Laravel Project

If you already have a Laravel project, you can skip this step. Otherwise, open your terminal and run the following command to create a new Laravel project:

composer create-project --prefer-dist laravel/laravel show-hide-div

Once the installation is complete, navigate to the project directory:

cd show-hide-div

Now, launch the Laravel development server using the following command:

php artisan serve

You can now access your new Laravel 10 project in your web browser at http://localhost:8000.

Step 2: Creating the HTML Markup

In this step, we will create a basic HTML structure for our demo. Open the resources/views/welcome.blade.php file and replace its content with the following code:

<!DOCTYPE html>
<html>
<head>
    <title>Show/Hide Div with Radio Button - LaravelTuts.com</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
    <h1>Show/Hide Div with Radio Button - LaravelTuts.com</h1>

    <div>
        <input type="radio" name="show_hide" value="show"> Show
        <input type="radio" name="show_hide" value="hide"> Hide
    </div>

    <div id="content" style="display: none;">
        This div will be shown/hidden based on the radio button selection.
    </div>

    <script>
        $(document).ready(function() {
            $('input[type="radio"]').click(function() {
                var value = $(this).val();
                if (value === 'show') {
                    $('#content').show();
                } else {
                    $('#content').hide();
                }
            });
        });
    </script>
</body>
</html>

Step 3: Understanding the Code

Let’s go through the code to understand how it works:

  • We have two radio buttons with the same name attribute (show_hide). These radio buttons allow the user to choose whether they want to show or hide the content.
  • The content div is initially hidden using the inline CSS style display: none;.
  • We use jQuery to handle the click event on the radio buttons. When a radio button is clicked, we check its value. If the value is “show,” we show the content div by calling the show() function. If the value is “hide,” we hide the content div using the hide() function.

Step 4: Testing the Functionality To test our code, start the Laravel development server by running the following command in your terminal:

php artisan serve

Open your browser and visit http://localhost:8000. You should see the “Show/Hide Div with Radio Button” heading and two radio buttons. Initially, the content div will be hidden.

Try selecting the “Show” radio button. You should see the content div appear below the radio buttons. Similarly, selecting the “Hide” radio button will hide the content div.

Congratulations! You have successfully implemented the show/hide functionality using Laravel and jQuery.

Conclusion:

In this tutorial, we explored how to create a Laravel jQuery show/hide div functionality using radio buttons. By following the steps outlined above, you should now have a basic understanding of how to implement this feature in your Laravel projects.

The key takeaway from this tutorial is the ability to leverage the power of jQuery to manipulate the visibility of HTML elements based on user interactions. This technique can be applied to various scenarios where you want to dynamically show or hide content on your web pages.

Remember to adapt and customize the code to suit your specific project requirements. You can enhance the functionality by adding additional radio buttons or extending the JavaScript code to handle more complex interactions.

By combining Laravel’s server-side capabilities with jQuery’s client-side scripting, you can create dynamic and interactive web applications that provide a seamless user experience.

We hope this tutorial has been helpful in your journey to create dynamic web interfaces with Laravel and jQuery. Feel free to explore further and experiment with different features to enhance your projects. Happy coding!

Share this:

Categorized in: