Stay put, conceal, and revert selected option according to the button

Apologies for the confusion with the title of this section.

I want to hide certain select options until a user selects a specific button. Once a button is selected, I want the corresponding select field to remain visible. However, if the user chooses a different button, I want to reset the previously selected options and hide them again.

$(document).ready(function() {
  $(':radio').on('change', function() {
    var title = $(this).val(); // Get radio value
    if ($(this).attr('id') === 'theme2') {
      $('.occ_select').addClass('l').removeClass('off');
    } else { 
      $('.occ_select').removeClass('l').addClass('off');
    }

    $("#occa_slct option").eq(0).prop("selected", title == "dedi") 
    $("#occa_slct").prop("disabled", title == "dedi")
  });
});

$(document).ready(function() {
  $(':radio').on('change', function() {
    var title = $(this).val();
    if ($(this).attr('id') === 'shape1') {
      $('.c_select').addClass('l').removeClass('off');
    } else {
      $('.c_select').removeClass('l').addClass('off');
    }

    $("#c_slct option").eq(0).prop("selected", title == "heart")
    $("c_slct").prop("disabled", title == "heart")
  });
});

$(document).ready(function() {
  $(':radio').on('change', function() {
    var title = $(this).val();
    if ($(this).attr('id') === 'shape2') {
      $('.h_select').addClass('l').removeClass('off');
    } else {
      $('.h_select').removeClass('l').addClass('off');
    }

    $("#h_slct option").eq(0).prop("selected", title == "circle")
    $("h_slct").prop("disabled", title == "circle") 
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<hr>
Choose Theme:
<br> <input type="radio" id="theme1" name="cake_theme" value="dedi" /> Dedication
<br> <input type="radio" id="theme2" name="cake_theme" value="occ" /> Others


<div id="occassion" class="occ_select off">
  <select name="occassion_select" id="occa_slct">
     <option disabled selected value>-- Select one --</option>
     <option value="otheerrr"> otheerrr </option>
     <option value="other1"> other1 </option>
     <option value="other2"> other2 </option>
 </select>
</div>
<hr>

Choose Shape:
<br> <input type="radio" id="shape1" name="shape" value="circle" /> Circle

 <div id="circle" class="circle_select off">
  <select name="c_select" id="c_slct">
     <option disabled selected value>-- Select one --</option>
     <option value="big"> big </option>
     <option value="med"> med </option>
     <option value="small"> small </option>
 </select>
</div>

<br> <input type="radio" id="shape2" name="shape" value="heart" /> Heart

 <div id="heart" class="heart_select off">
  <select name="h_select" id="h_slct">
     <option disabled selected value>-- Select one --</option>
     <option value="big"> big </option>
     <option value="med"> med </option>
     <option value="small"> small </option>
 </select>
</div>

<hr>

It seems there may be some confusion or a bug occurring in this section. Feel free to reach out if you need clarification on my request. Thank you in advance for any help provided!

Answer №1

Are you seeking this particular information?

  • When the radio button is changed, check if the selected radio button belongs to the theme or shape category using the name attribute.
  • If it's related to choosing a shape, then determine whether it's shape1 or shape2.
  • If it's shape1, set the display property of the shape1 select box (#c_select) to block and the shape2 select box (#h_select) to none, and vice versa. Also, reset the default selection of #h_select. Apply the same method for choosing theme radio buttons.
  • If desired, on document ready, set the default value for all select boxes using the $("selector").each function.

Note: It is not necessary to separate every logic into individual document-ready functions (refer to DRY)

$(document).ready(function() {
  $("select").each (function() {
    $(this).find ('option:eq(1)').prop('selected', true); 
  });
  $(':radio').on('change', function() {
    var title = $(this).val(); // Get radio value
    if ($(this).attr ("name") === "cake_theme") {
      if ($(this).attr('id') === 'theme2') {
        $('.occ_select').css ({"display": "block"});
      } 
      else if ($(this).attr('id') === 'theme1') { 
        $('.occ_select').css ({"display": "none"});
        $('.occ_select option:eq(1)').prop('selected', true);
      } 
    }
    else if ($(this).attr ("name") === "shape") {
      if ($(this).attr('id') === 'shape1') {
        $('#c_slct').css ({"display": "block"});
        $('#h_slct').css ({"display": "none"});
        $('#h_slct option:eq(1)').prop('selected', true);
      } 
      else if ($(this).attr('id') === 'shape2') {
        $('#c_slct').css ({"display": "none"});
        $('#c_slct option:eq(1)').prop('selected', true);
        $('#h_slct').css ({"display": "block"});
      } 
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<hr>
Choose Theme:
<br> <input type="radio" id="theme1" name="cake_theme" value="dedi" /> Dedication
<br> <input type="radio" id="theme2" name="cake_theme" value="occ" /> Others


<div id="occassion" class="occ_select off">
  <select name="occassion_select" id="occa_slct">
     <option disabled selected value>-- Select one --</option>
     <option value="otheerrr"> otheerrr </option>
     <option value="other1"> other1 </option>
     <option value="other2"> other2 </option>
 </select>
</div>
<hr>

Choose Shape:
<br> <input type="radio" id="shape1" name="shape" value="circle" /> Circle

 <div id="circle" class="circle_select off">
  <select name="c_select" id="c_slct">
     <option disabled selected value>-- Select one --</option>
     <option value="big"> big </option>
     <option value="med"> med </option>
     <option value="small"> small </option>
 </select>
</div>

<br> <input type="radio" id="shape2" name="shape" value="heart" /> Heart

 <div id="heart" class="heart_select off">
  <select name="h_select" id="h_slct">
     <option disabled selected value>-- Select one --</option>
     <option value="big"> big </option>
     <option value="med"> med </option>
     <option value="small"> small </option>
 </select>
</div>

<hr>

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

Retrieve a true value by using either Array.some or _.some

I am looking to retrieve a truthy value from the array.Some other than "true", This is my approach: let category; arduair.aqi_ranges[pollutant].some((item,index)=> { let min = item.range[0]; let max = item.range[1]; if (_.inRange(c,min,max)){ ...

Increase the value of a property within an array of objects based on certain conditions

My task involves incrementing the rank value by one until the name property changes. I am utilizing the page and rank properties to track when this change occurs. In addition, I want to increment it once whenever the type is not equal to none, and then re ...

Best practices for executing an asynchronous forEachOf function within a waterfall function

I've been working with the async library in express js and I'm encountering an issue when using two of the methods alongside callbacks. The variable result3 prints perfectly at the end of the waterfall within its scope. However, when attempting t ...

Bookeo API allows only a maximum of 5 requests to be processed through Node.js

Greetings! I am excited to be posting my first question here, although I apologize in advance if anything appears unclear! My current project involves utilizing Bookeo's API to extract booking data from emails belonging to a tour company and transfer ...

Retrieve the HTML content of all children except for a specific child element in jQuery

Is there a way to utilize jQuery/Javascript for selecting the HTML content of the two <p> elements in the initial <div class="description? I'm open to using Regex as well. This specific jQuery selection is being executed within Node.js on a c ...

When using this.$refs in Vue, be mindful that the object may be undefined

After switching to TypeScript, I encountered errors in some of my code related to: Object is possibly 'undefined' The version of TypeScript being used is 3.2.1 Below is the problematic code snippet: this.$refs[`stud-copy-${index}`][0].innerHTM ...

Tips for ensuring a consistent highlight for an active link in Bootstrap

Does anyone have a solution for maintaining highlighted links on a web page? <div id="productnav"> <nav> <ul class="nav"> <li><a href="<?php echo $prefix; ?>pages/one.php?category=1" id="navelement1"<?php if ($c == 1) e ...

What is the best way to transfer data between controllers in my particular situation?

Currently, I am working on developing a factory for the restful services. The main challenge I'm facing is how to transfer data from one controller to another in AngularJS. Specifically, I need to use the data obtained from the first service call to ...

Enhancing Accessibility for the jQuery Countdown Plugin

Seeking to enhance the accessibility of my website's jQuery countdown, I am striving to adhere to WAI-ARIA guidelines. The specified requirements are as follows: Ensure the area is live so it updates dynamically with the countdown display. Avoid re ...

What are some solutions for repairing unresponsive buttons on a webpage?

My task is to troubleshoot this webpage as the buttons are not functioning correctly. Here’s a snippet of the source code: <!DOCTYPE html> <html lang="en"> <head> ... </head> <body> <div id="container" ...

What is the best way to display an error message for an invalid input using JavaScript?

I am struggling to implement error messages for my form inputs using JavaScript. My attempts so far have not been successful and I need guidance on the correct approach. I want to show an error message and prevent the form from being submitted if any erro ...

Guide on showing a message on the server side when pressing a button in the web browser

I need the following question to function seamlessly on all major browsers including Opera, Internet Explorer, Chrome, Safari, and Firefox I am working on an application that requires users to follow a specific order of pages Text1.php, Text2.php, Text3.p ...

What is the method for acquiring the final shape of a particular type?

Is there a way to locate the last path or circle in a paper so that I can perform additional calculations to add more elements? Currently, calling paper.bottom only retrieves the last element. Is it possible to access shapes of specific types, like bottom. ...

Creating a bower.json file similar to requirements.txt

After diving into the world of bower, I've found it to be incredibly useful. Coming from a python background, I'm accustomed to working with virtualenv and requirements.txt. Given that I prefer not to keep all my dependencies in source control, ...

Challenges associated with Bootstrap toggle switch

I am having some difficulties with using bootstrap switch. I have tried the examples provided on , but regardless of the classes I apply, the buttons remain the same small size and the text for ON and OFF is barely visible. I can't seem to figure out ...

How to remove a variable definition in Typescript

Is there a way to reset a variable to undefined after assigning it a value? To check, I am using the Underscore function _.isUndefined(). I have attempted both myVariable = undefined and delete myVariable without success. ...

What could be the reason for the Puppeteer script returning 'undefined' when using ExecutionContext.evaluateHandle()?

Currently, I am in the process of learning the Puppeteer API with version 1.9.0. Below is the code I am using to click a button within an iframe: const changePrefsFromAllToNone = async () => { try { const browser = await puppeteer.launch ...

Changing the material color in Three.js from black to transparent

Looking to create a Lambert material using a texture with transparent black color, without using an alphaMap. I have a sphere with a clouds texture that I want to appear transparent black. When I try using blending, it affects the shadows, and I want to m ...

Async await expressions route hung

I have been in the process of transitioning my application to utilize async/await instead of callbacks for query requests on the backend. Progress has been smooth so far, but I have encountered a problem. When trying to fetch a get route, my page is hangin ...

How do I transform the date "Tue Nov 26 2019 16:00:00 GMT-0800" into the .NET JSON date format "/Date(1574812800000)/"?

I am looking to convert a date from ISO format to .NET JSON date format using JavaScript. For example, I want to change "Tue Nov 26 2019 16:00:00 GMT-0800" to "/Date(1574812800000)/". ...