Display/Conceal/Inactivate form input and division

I am utilizing the code snippet below to display a div and disable certain input values based on selection changes. The choices are Approval, Request, and Change. I suspect there is an issue with the third set of form options in my code. How can I modify it to disable inactive options? What could be causing errors in my script?

var Permissions = jQuery('#privileges');
var select = this.value;
Permissions.change(function() {
  if (jQuery(this).val() == 'approval') {
    jQuery('.approval').show();
    jQuery('#requesttype').prop('disabled', true);
    jQuery('#requestpurpose').prop('disabled', true);
    jQuery('#busformdoc4').prop('disabled', true);
    jQuery('#busformdoc3').prop('disabled', true);
    jQuery('#requestsendit').prop('disabled', true);
  } else jQuery('.approval').hide();
  if (jQuery(this).val() == 'request') {
    jQuery('.request').show();
    jQuery('#approvaltype').prop('disabled', true);
    jQuery('#where').prop('disabled', true);
    jQuery('#busformdoc1').prop('disabled', true);
    jQuery('#busformdoc2').prop('disabled', true);
    jQuery('#approvalsendit').prop('disabled', true);
  } else jQuery('.request').hide();
  if (jQuery(this).val() == 'change') {
    jQuery('.change').show();
    jQuery('#changes').prop('disabled', true);
    jQuery('#busformdoc5').prop('disabled', true);
    jQuery('#busformdoc6').prop('disabled', true);
    jQuery('#changesendit').prop('disabled', true);
  } else jQuery('.change').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <select name="permissions" id="privileges" class="">
    <option disabled selected>– Select your request type –</option>
    <option id="approval" value="approval">Approval Required</option>
    <option id="request" value="request">Request for Design</option>
    <option id="change" value="change">Change Artwork Request</option>
  </select>
</div>
<div class="approval" style=" display: none;">
  <select name="type" id="approvaltype" required>
    <option disabled selected>– Type –</option>
    <option value="1">1</option>
  </select>
  <textarea name="where" id="where" title="Purpose" required></textarea>
  <input type="file" name="busformdoc1" id="busformdoc1">
  <input type="file" name="busformdoc2" id="busformdoc2">
  <input name="sendit" id="approvalsendit" type="submit" value="Send Approval Request" />
</div>
<div class="request" style=" display: none;">
  <select name="type" id="requesttype" required>
    <option disabled selected>– Type –</option>
    <option value="1">1</option>
  </select>
  <textarea name="purpose" id="requestpurpose" title="purpose" required></textarea>
  <input type="file" name="busformdoc3" id="busformdoc3" >
  <input type="file" name="busformdoc4" id="busformdoc4" >
  <input name="sendit" id="requestsendit" type="submit" value="Send Artwork Request"  />
</div>
<div class="change" style=" display: none;">
  <textarea name="changes" id="changes" title="Changes" required></textarea>
  <input type="file" name="busformdoc5" id="busformdoc5" >
  <input type="file" name="busformdoc6" id="busformdoc6" >
  <input name="sendit" type="submit" value="Send Change Request" class="btn" />
</div>

Answer №1

Providing some assistance here. When choosing a category, make sure to enable its elements and disable other category elements (which you overlooked).

function toggleDisable(ids)
{
  //console.log(ids)
    var elems = ['requesttype','requestpurpose','busformdoc4','busformdoc3','requestsendit','approvaltype','where','busformdoc1','busformdoc2','approvalsendit','changes','busformdoc5','busformdoc6','changesendit'];
    elems.forEach(function(id){
        jQuery("#"+id).prop("disabled",ids.indexOf(id)<0)
        //console.log(jQuery("#"+id).prop("disabled"))
    })
}
var Privileges = jQuery('#privileges');
var select = this.value;
Privileges.change(function() {
  if (jQuery(this).val() == 'approval') {
    jQuery('.approval').show();
    toggleDisable(['approvaltype','where','busformdoc1','busformdoc2','approvalsendit']);
  } else jQuery('.approval').hide();
  
  if (jQuery(this).val() == 'request') {
    jQuery('.request').show();
    toggleDisable(['requesttype','requestpurpose','busformdoc4','busformdoc3','requestsendit']);
  } else jQuery('.request').hide();
  
  if (jQuery(this).val() == 'change') {
    jQuery('.change').show();  toggleDisable(['changes','busformdoc5','busformdoc6','changesendit']);
  } else jQuery('.change').hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <select name="privileges" id="privileges" class="">
    <option disabled selected>– Select what you are requesting –</option>
    <option id="approval" value="approval">Advertising Approval</option>
    <option id="request" value="request">Artwork Design Request</option>
    <option id="change" value="change">Artwork Change Request</option>
  </select>
</div>
<div class="approval" style=" display: none;">
  <select name="type" id="approvaltype" required>
    <option disabled selected>– Type –</option>
    <option value="1">1</option>
  </select>
  <textarea name="where" id="where" title="Purpose" required></textarea>
  <input type="file" name="busformdoc1" id="busformdoc1">
  <input type="file" name="busformdoc2" id="busformdoc2">
  <input name="sendit" id="approvalsendit" type="submit" value="Send Approval Request" />
</div>
<div class="request" style=" display: none;">
  <select name="type" id="requesttype" required>
    <option disabled selected>– Type –</option>
    <option value="1">1</option>
  </select>
  <textarea name="purpose" id="requestpurpose" title="purpose" required></textarea>
  <input type="file" name="busformdoc3" id="busformdoc3" >
  <input type="file" name="busformdoc4" id="busformdoc4" >
  <input name="sendit" id="requestsendit" type="submit" value="Send Artwork Request"  />
</div>
<div class="change" style=" display: none;">
  <textarea name="changes" id="changes" title="Changes" required></textarea>
  <input type="file" name="busformdoc5" id="busformdoc5" >
  <input type="file" name="busformdoc6" id="busformdoc6" >
  <input name="sendit" type="submit" value="Send Change Request" class="btn" id="changesendit" />
</div>

Answer №2

Why are you utilizing jQuery in such a manner? You seem to have missed the main purpose of jQuery, which is to manipulate HTML elements. Your current code seems to be manipulating yourself instead. Clean up the redundancy and give it another shot.

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 UI bootstrap dropdown toggle requires two clicks to reopen after being manually closed

Utilizing the UI Bootstrap drop-down element to display the calendar from angular-bootstrap-datetimepicker upon clicking. Additionally, a $watch has been implemented to close the dropdown once a date is chosen. Access the Plunker here <div uib-dropdow ...

Ensure that the <td> element remains within the confines of the window

When creating a table with some content, I encountered an issue where long strings would extend beyond the window. I attempted to set max-width: 80%; to limit the length, and also tried setting td, th = width: 240px;, but the problem persisted. Next, I ex ...

Adding a stylish background image to a header

Recently, I tried to add a background image as a backdrop for my Header but ran into some issues. I was successful using a background color, but not with the background image itself. h2 { background-color: red; font-family: "Comic Sans MS", cursiv ...

A step-by-step guide on configuring data for aria's autocomplete feature

Currently, I am implementing aria autocomplete and facing an issue while trying to populate data from the server into the selection of aria autocomplete. I have tried setting the selected property of the aria autocomplete object, but it doesn't seem t ...

Choosing a single random key-value pair from a specific attribute within a JSON schema

Is there a way to generate a new JSON schema based on the existing one, but with only one key-value pair chosen randomly from the "properties" attribute? The new schema should retain the "title" and "type" attributes as well. { "title": "animals object" ...

Tips for integrating an HTML template into a React project

I'm finding it challenging to integrate an HTML template into React. The template I am using is for an admin dashboard with multiple pages. I have successfully copied and pasted the HTML code into JSX files and fixed any syntax issues. Here's wh ...

The CSS code designed to limit the display to only a two-line sentence is failing to work properly in Internet Explorer

I've implemented the following CSS code to ensure that the display sentence doesn't exceed 2 lines, with the remaining text being replaced by "...". .bell-notification-max-words-display { overflow: hidden; display: -webkit-box; ...

jQuery mobile not recognizing the ID we specified

I am in the process of developing an audio application. My goal is to change the id of the Play button dynamically to "paused" when it is clicked. However, despite my efforts, clicking on the "paused" button does not pause the audio as intended. $(&ap ...

Toggle the visibility of fields using dynamic identifiers (NodeJS, Mongoose, Express, JavaScript)

I am using a forEach loop to display menus for various restaurants when the restaurant icon is clicked. The number of restaurants/menus and their icons are unknown, each created with a dynamic ID such as menu-0, menu-1, menu-2, and so on. When one restaur ...

AngularJS text markers

In order to streamline the process of managing tags with random content, I have devised a 'tag' manipulation system using the angular-ui alert mechanism. The system includes a factory and a directive as follows: Factory: app.factory( &a ...

What are the steps to create a dynamic background image that adjusts to various

When viewing the website on a computer screen, the entire cat is visible in the background image. However, when switching to a mobile screen size, only a portion of the cat is displayed, appearing as though it has been cut off. The image is not smaller on ...

Whenever I attempt to include additional drop-down lists, jQuery fails to function

My webpage features multiple drop down lists, with one populating based on the selection from another. Additionally, I have included a button at the bottom of the page. When this button is clicked, I need to add another column to the page. However, after ...

Angularjs - Repeatedly reloading identical images

Whenever I use ng-src in the <img> tag and change the ng-src by clicking next or previous buttons, the browser reloads the image even though it's already loaded. This results in a not-so-smooth experience as the image is downloaded again before ...

Gradually make the table row and its contents disappear as you scroll, based on how hidden the row is within the containing div

Uncertainty clouds my mind as to the practicality of this approach, the potential performance implications, and the frequency of scroll events firing to make this technique viable. Nevertheless, within a fixed height div (overflow-y: auto) lies a table. M ...

Utilize parameters in specified file in node.js

Currently, I am attempting to send variables to a file that I have imported in node.js in order to utilize them within that specific file. var myname = "Adnan"; var incfile = require('./ex.js'); My main goal is to access the myname variable wit ...

Is it possible for Carousel to showcase a different layout other than tables or items? And is there a way to customize

I'm trying to use Bootstrap 5 to display items in a row, like sets of 3 or 12, and have them slide into view one set at a time. However, when I add a carousel, the items end up stacked on top of each other. Here is the code snippet: <div c ...

What is the best way to create a reusable component for Cards and implement the new makeStyles feature in material-ui?

Within the depths of my demo.js file, I desire to utilize the Card component with its nested CardHeader and fill in some text within the CardContent. Furthermore, I aim to make the CardComponent easily reusable across multiple files. Any suggestions on how ...

Tips for utilizing JSON in your response to an AJAX request

Recently, I developed a message inbox feature where clicking on the message title triggers an AJAX call. Now I am looking for guidance on how to handle JSON responses from the server-side. Is there a way to reply to the call using JSON and effectively ext ...

Assist in HTML to exhibit the display name property devoid of a label

Here is the code snippet I am working with: [Display(Name = "Empresa")] public string Company{ get; set; } In my ASPX file, I have: <th><%: Html.LabelFor(model => model.Company)%></th> This code generates the following output: < ...

The Ajax form is failing to send any headers

Whenever I submit my form, the header data doesn't seem to be coming through. Even though I've done this type of submission numerous times (figuratively speaking), there's always a chance that I might be overlooking something. Any ideas? Che ...