Conceal a form field depending on the data retrieved from a database in PHP

I need assistance with hiding an input field based on the chosen value from a dropdown. There are two values, stone1 and stone2, that are automatically populated from a database. My goal is to hide the "karat" input field if stone1 is selected, hide the "carat" input field if stone2 is selected, and hide both fields if any other value is selected.

<label for="form" class="control-label">=Service</label>
<select id="form1" class="custom-select custom-select-sm select2">
   <option selected="" disabled>Select services First</option>
   <?php
      ?>
   <!-- <option value="<?php echo $row['id'] ?>" ><?php echo $name ?></option> -->
   <?php endwhile; ?>
   <div class="col-md-2">
      <div class="form-group ctd" data-cid="ctd">
         <label for="ctd">stone1</label>
          <select class="form-control" id="ctd" name="ctd">
             <option>24</option>
             <option>22</option>
             <option>20</option>
             <option>18</option>
             <option>16</option>
             <option>14</option>
           </select>
        </div>  
     </div> 
       <div class="col-md-2">
            <div class="form-group ctg" data-cid="ctg">
                 <label for="ctg">stone2</label>
                        <select class="form-control" id="ctg" name="ctg">
                           <option>24</option>
                            <option>22</option>
                             <option>20</option>
                              <option>18</option>
                               <option>16</option>
                                <option>14</option>
                         </select>
                    </div>
                </div>

The auto-populated values are stone1 and stone2 from the database. If stone1 is selected, I want the karat input field to be hidden. If stone2 is selected, I want the carat input field to be hidden. Additionally, if any other value is selected, both input fields should be hidden.

I attempted the code provided below, but it only hides one of the input fields and does not work properly:

<script>    
  var form1 = jQuery('#form1');
  var select = this.value;
  form_id.change(function () {
    if ($(this).val() == 'stone1') {
      $('.ctd').show();             
    } else {
      $('.ctg').hide(); 
    }               
  });       
           
</script>

Answer №1

     (function ($) {
 
        $(document).ready(function () {
            var form = $('#form1');
            var selectValue = this.value;
            
            $('#form1').change(function(){
                if ($(this).val() === "1") {
                    $("#ctd").show();
                    $("#ctg").hide();
                    $('label[for="ctg"]').hide();
                    $('label[for="ctd"]').show();
                } else {
                    $("#ctg").show();
                    $("#ctd").hide();
                    $('label[for="ctd"]').hide();
                    $('label[for="ctg"]').show();
                }
            });
 
            $('#form1').trigger("change");
         });
 
     }(jQuery));

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

The AJAX calendar control remains unchanged after a reset

I am currently facing an issue with my ajax calendar control used in a form for selecting dates. The problem arises when I select a date from the previous year and then click on the reset button. Even though the text box is cleared, the calendar control s ...

Creating a dynamic table based on selected dropdown option

I need to create a dynamic table of questions depending on the user's selection from a dropdown menu. When the user chooses a category, I want to pass that information to an API using the GET method. My controller is built using AngularJS. However, I ...

The click event fails to trigger when a <tr> element is dynamically appended via jQuery ajax

Hi there, I'm encountering an issue with the click event of jQuery. In my ajax function, I am dynamically adding a table row that includes a button with the class btn btn-default getEditCategory, but unfortunately, it is not working as expected. Belo ...

No matter how tall I make it, the scroll bar just won't stop showing

Check out this fiddle for more information: http://jsfiddle.net/LAueQ/ Even after increasing the height of the campaignDiv div, the scrollbar keeps showing up. If possible, I would like to avoid using overflow: hidden. ...

Debugging Success Message Display Issue in JavaScript

$.ajax({ type: "POST", url:'/saveLayout', data: {id : layoutId, section : arrSection}, success: function(response){ $('#successMsg').addClass("errorBox"); document.getEleme ...

Having trouble incorporating Duo Web SDK into angular application

We are currently working on incorporating Duo Two-factor authentication into our Angular application. For instructions, you can take a look at the documentation available here. The issue we are encountering is that their JavaScript file searches for an i ...

Adjustable Title in Line Plot

After receiving a JSON object from a web server in response, I am encountering some challenges with extracting and displaying the data. The JSON array consists of monthly key-value pairs like {"JAN":"17"}, {"FEB":"19"}, {"MAR":"21"}, etc. To display these ...

Create a Vue3 Component without attaching it to the DOM

Let's consider creating a Vue3 component in the following way: import { defineComponent } from "vue"; var instance = defineComponent({ computed:{ message() { return 'Hello world' } } }) To instantiate it and ...

Angular modifies the structure of a JSON string

Currently, I am attempting to display data on my screen utilizing AngularJS, ASP.NET, and JSON.NET. I have noticed that when I make a call to $.getJSON, it returns a standard JSONArray. However, when I assign this data to a variable inside my Angular contr ...

Getting to grips with accessing HTML elements within a form

<form name="vesselForm" novalidate> <input type="text" id="owner" name="ownerEdit" required ng-blur="vesselForm.btnCC.attr('value', 'Change Customer')"/> <input type="button" name="btnCC" value="Customer" /> </fo ...

Styling the center menu

Is there a way to horizontally center this top menu using CSS? https://i.sstatic.net/9yvJf.png This is the HTML structure I am working with: <div class="wrapper_menu_hoz"> <div class="container_24 "> <div class="gr ...

Steps to enable editing in an array

I'm struggling to implement an editing functionality for the lists inside the ul element in my simple to-do list project. Currently, I have the delete functionality working but need guidance on how to add an edit function. The list items are added by ...

Mongoose Filtering in Rest API: A Comprehensive Guide

Currently, I am utilizing Express to construct an API. Within this API, my objective is to retrieve a list of customers from MongoDB by using Mongoose. The code snippet below showcases the route I have set up (please disregard any paging and limit concerns ...

Is it possible to override Bootstrap or not?

It's quite a puzzle to figure out when I can and cannot override Bootstrap classes or IDs with custom CSS. For instance, it seems easy enough to change the default navbar-dark background and font style, but altering the font color and size proves to ...

Generate a variety of files using GraphicsMagick

I'm trying to enhance my function that deals with uploaded images. Currently, it captures the image, converts it, and saves only one version of it on the server. However, I would like to modify it to achieve the following goals: Goals: Save multipl ...

Preventing the page from scrolling to the top while utilizing an Angular-bootstrap modal and a window position:fixed workaround on an iPad

It's common knowledge that there is a bug in bootstrap modals on certain devices, causing the page behind the modal to scroll instead of the modal itself (http://getbootstrap.com/getting-started/#support-fixed-position-keyboards) An easy fix for this ...

What is the most effective method for flipping a THREE.Sprite in r84?

After updating my game engine from r69 to r84, I encountered a few issues. One of them is related to flipping sprites. In the past, I could easily flip sprites using the following code: sprite.scale.x = -1; Unfortunately, this method no longer works in ...

An issue arises when trying to loop using a while loop within an HTML structure

I have a small project with a predefined format, where I need to select a value from a dropdown menu and use that value to fetch data from a database and display it in HTML. While I am able to retrieve the data from the database based on the selected value ...

VeeValidate fails to validate input fields in a form that is constantly changing

My goal is to create dynamic forms with validations using veeValidate in Vue.js. I am attempting to achieve this by storing an array of objects within the component's data. For instance: data(){ return{ inputs: [ { id: 1, lab ...

Issue with showing an input field following the button click

I'm having a bit of trouble with this function that should add an input element to a div. I've tried using appendChild and also concatenating the object to innerHTML, but no luck so far. Could it be something my beginner's mind is missing? f ...