What's the reason the element inside the <div> is not displayed when a value is selected in the dropdown box?

I am perplexed as to why the element inside the does not appear when a value is selected in the drop down box. However, it works perfectly fine in JSFiddle: JSFiddle

Below is my HTML code and Jquery script that functions properly in my own system:

<tr>
                <th class="title">Registration</th>
                <td> : </td>
                <th><select name="reg" id="registration">
                        <option value="No">No</option>
                        <option value="Yes">Yes</option>
                    </select>
                </th>
            </tr>
            <div id="opt" style="display:none;">
            <tr>
                <th class="title">Participant</th>
                <td> : </td>
                <th><input type="text" name="participant"></th>
            </tr>

            <tr>
                <th class="title">Payment Amount</th>
                <td> : </td>
                <td><input type="text" name="amount"></td>
            </tr>

            <tr>
                <th class="title">Payment Method</th>
                <td> : </td>
                <td><input type="radio" name="pay_method" value="CC">Credit Card
                <input type="radio" name="pay_method" value="Counter">Counter
                </td>
            </tr>
            </div><!--end of the opt-->

Jquery script:

<script type="text/javascript">
$(document).ready(function(){

 $("#registration").change(function(){
 if($(this).val()=="Yes"){
            $("#opt").show();
       }else {
        $("#opt").hide();   
       }

 });      
}); 

   </script>

The expected output is as follows:

If the value "No" is selected, nothing should be displayed.

Answer №1

If this represents the finalized structure of your table, consider eliminating the #opt element and using this alternative approach:

<script type="text/javascript>
$(document).ready(function(){

 $("#registration").change(function(){
 if($(this).val()=="Yes"){
            $("tr + tr").show();
       }else {
        $("tr + tr").hide();   
       }

 });      
}); 
</script>

Another suggestion is to utilize <tbody>:

    <tr>
            <th class="title">Registration</th>
            <td> : </td>
            <th><select name="reg" id="registration">
                    <option value="No">No</option>
                    <option value="Yes">Yes</option>
                </select>
            </th>
        </tr>
        <tbody id="opt" style="display:none;">
        <tr>
            <th class="title">Participant</th>
            <td> : </td>
            <th><input type="text" name="participant"></th>
        </tr>

        <tr>
            <th class="title">Payment Amount</th>
            <td> : </td>
            <td><input type="text" name="amount"></td>
        </tr>

        <tr>
            <th class="title">Payment Method</th>
            <td> : </td>
            <td><input type="radio" name="pay_method" value="CC">Credit Card
            <input type="radio" name="pay_method" value="Counter">Counter
            </td>
        </tr>
        </tbody><!--End of opt-->

 <script type="text/javascript>
 $(document).ready(function(){

  $("#registration").change(function(){
  if($(this).val()=="Yes"){
            $("#opt").show();
       }else {
        $("#opt").hide();   
       }

  });      
 }); 
</script>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Avoiding duplicate touch events with an if statement

I am currently working on a module for a responsive website that involves tapping the initial screen to reveal a panel from the right. The user can then tap a close button to hide the panel. However, there is an issue where if the user taps multiple times ...

The issue of JQuery recursion not properly focusing on a textbox

Can anyone help with a jquery focus issue I'm experiencing? My goal is to address the placeholder problem in IE by focusing on an element and then blurring it to display the placeholder. This functionality is being used in a modal form. Initially, e ...

Unable to view images on Wordpress theme

I am currently facing an issue where some images in my asset folder are not displaying properly when I convert my HTML/CSS/JS template to Wordpress. The main problem is with the image that should show up when you first visit the website. Below is the CSS c ...

Is there a way to increase the total of each row by two when a button is clicked?

Looking to enhance the sum of each row by two depending on whether a button is clicked. HTML: <table> <tr> <td> <input type="checkbox" class="prof" name="prof" value="0"> <input class=&quo ...

Attempting to link two JavaScript files, however, only one of them is functioning correctly

I'm currently experiencing an issue with my HTML page that involves calling two JS files for two different image sliders on the same website page. One slider works perfectly fine while the other does not. I'm confused as to whether it's perm ...

The switch switches on yet another switch

Greetings everyone, Currently, I am in the midst of my exam project and creating a mobile menu. The functionality is there, but unfortunately, when closing the menu, it also triggers the search toggle which displays an unwanted div, becoming quite botherso ...

What is the best way to title an uploaded chunk with HTML5?

Here is the script I am working with: function upload_by_chunks() { var chunk_size = 1048576; // 1MB function slice(start, end) { if (file.slice) { return file.slice(start, end); } else if (file.webkitSlice) { ...

The state of XMLHttpRequest always remains in a perpetual state of progress, never

I have come across an MVC Core application. One of the methods in this application currently has the following structure: public IActionResult Call(string call) { Response.ContentType = "text/plain"; return Ok(call); } In addi ...

Challenges with Internal Styling on Wordpress Sites

Okay. I've been dealing with some internal style issues on my Wordpress website. After dedicating hours to trying to change styles for various elements, I realized that the main html file contained a bunch of internal style sheets that were overridin ...

Arranging divs with HTML and CSS

I am currently facing issues with the positioning of 3 scripts in my HTML file. The vertical gauge is overlapping the second circular gauge, despite trying different positioning options to no avail. How can I adjust the layout so that the vertical gauge a ...

The FAB button animation is causing delays in the transition process and is not functioning as originally anticipated

I am facing an issue with the FAB button and 3 Icons. The functionality is working fine on click for both show and hide actions, but the transition is too delayed. I want the icons to appear step by step, even though I have adjusted the transition delay se ...

A guide to activating tag selection within the DevExtreme tag box

I'm currently utilizing devExtereme within my Angular project. My goal is to enable the selection of text within tags in my tagbox component. Here's what I have implemented: <dx-tag-box [dataSource]="sourves" [value]="value&quo ...

ajax request within a function

Could you assist me with this issue? When the success function is triggered, I am able to obtain the necessary result. However, the ajaxFunction ultimately returns an empty value. ajaxFunction = function(lastNum){ var json_res = ''; ...

Using jQuery Validation Plugin - Activate 'Eager' Validation only upon an unsuccessful submission

Is there a way to toggle the 'eager' validation in a form before and after submission? I attempted to use $.validator.setDefaults() initially with onkeyup and onfocusout properties set to false. In the invalidHandler method, I reset onkeyup and ...

Guide on toggling the display of a div in JQuery

Hi everyone! I'm trying to figure out how to close a div that is open with the show() function. For example, I have a "div a" containing a hidden "div b". When I click on "div a", "div b" should appear. However, when I click on an [x] inside "div b", ...

What is the best way to access a specific element within a component from a different component?

Seeking assistance with communication issues between React components. I have a Container component containing child components Contact, More, and About for a single-page website. Each child component has a reference set. The problem arises when trying to ...

Can someone assist me in populating my dropdown menu with the data from my JSON response?

I have received JSON data from a C# web method in the following format: {"d":["ADAMS CITY","BOULDER","ANTON","ARBOLES"]} There is an ASP.NET dropdown with the ID #city. After receiving a success alert on my AJAX request, how can I display this data in th ...

Uploading images in PHP

I need assistance with uploading an image to a server using PHP, saving it inside a directory, and then retrieving the image URL. Here is my HTML code: <form method="post> <fieldset> <div class="form-group col-md-6 col-lg-6 col-sm-1 ...

What is the best way to assign a unique number to every div id that is generated?

I am currently using a django for loop to retrieve data from a query set. As the information is displayed, I would like to have each item wrapped in a div tag with a unique id that increments by 1 for every instance. Is there a way to achieve this directly ...

Is there a way to display the output of jQuery in an input box rather than a span?

I need to display the result of this function in an input box instead of a span tag because I plan to utilize the result in the following form. function checkChange(){ $.ajax({ url: "ordercalculate.php", data: $('form').seria ...