Difficulty with displaying or concealing Javascript code

After stumbling upon this script online, I discovered that it is designed to show/hide text boxes and labels. However, any alterations to the code seem to disrupt its functionality. For instance, changing divpassword will cause it to stop functioning altogether. The current problem lies in the fact that selecting "NO" hides everything thereafter. Is there a way to make it only hide the desired section, and how can I set the checkbox to default to "No"?

<script type="text/javascript>
  function DisplayHide() {
    var chkYes = document.getElementById("chkYes");
    var dvPassport = document.getElementById("dvPassport");
    dvPassport.style.display = chkYes.checked ? "block" : "none";
  }
</script>

<span>Are you pursuing further education?</span>
<label for="chkYes">
       <input type="radio" id="chkYes" checked name="chkEducation" 
    onclick="DisplayHide()" />
       Yes
    </label>
<label for="chkNo">
       <input type="radio" id="chkNo" name="chkEducation" onclick="DisplayHide()"/> 
        No
    </label>

I attempted to conclude it with or but had no success. Thank you!

Answer №1

If you want to make "no" the default option, you can do so by setting the input type="radio" with id="chkNo" as checked in the HTML code (currently, the checked element is the input type="radio" with id="chkYes"). Additionally, set the div with id="dvPassport" in the HTML to have a style of "display:none". The ShowHideDiv function appears to be functioning correctly. To hide only specific elements, ensure that the id="dvPassport" is applied to a div that contains ONLY the text boxes and labels. Here is an example:

<script type="text/javascript">
  function ShowHideDiv() {
    var chkYes = document.getElementById("chkYes");
    var dvPassport = document.getElementById("dvPassport");
    dvPassport.style.display = chkYes.checked ? "block" : "none";
  }
</script>

<span>Do you have More Education?</span>
<label for="chkYes">
       <input type="radio" id="chkYes" name="chkEducation" 
    onclick="ShowHideDiv()" />
       Yes
    </label>
<label for="chkNo">
       <input type="radio" id="chkNo" checked name="chkEducation" onclick="ShowHideDiv()"/> 
        No
</label>
<div id="dvPassport" style="display:none">
  <br/>
  <input type="text" placeholder="text1"/>
  <br/><br/>
  <input type="text" placeholder="text2"/>
<div>

Answer №2

If you want "no" to be the default option, all you have to do is transfer the word "checked" from the Yes input to the No input:

<input type="radio" id="chkNo" **checked** name="chkEducation" onclick="ShowHideDiv()"/> 

I placed your code inside a div with an ID of dvPassport to replicate the problem you are facing. One solution could be to use a section tag within the div surrounding your radio buttons. I added a paragraph tag after the section to show the impact (e.g., if someone switches from YES to NO and back)

Hopefully, this explanation clears things up for you

Sincerely, Emily

<script type="text/javascript>
  function ShowHideDiv() {
    var chkYes = document.getElementById("chkYes");
    //var dvPassport = document.getElementById("dvPassport");
    var education = document.getElementById("edu");
    //dvPassport.style.display = chkYes.checked ? "block" : "none";
    education.style.display = chkYes.checked ? "block" : "none";
  }
</script>

<div id="dvPassport">
 
 <section id="edu">    
    <span>Do you have More Education?</span>
    <label for="chkYes">
       <input type="radio" id="chkYes" name="chkEducation" 
    onclick="ShowHideDiv()" />
       Yes
    </label>
    <label for="chkNo">
       <input type="radio" id="chkNo" checked name="chkEducation" onclick="ShowHideDiv()"/> 
        No
    </label>
  </section>

  <p>hello</p>
  
</div>

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

Unable to close Bootstrap modal upon clicking "x" or "close" buttons

Hey everyone, I'm having a bit of trouble with my modal. It appears correctly when clicked to open, and the close buttons seem to detect that my mouse is hovering over them. However, when I click on the buttons, nothing happens and the modal remains o ...

What is the reason for the clearInterval consistently functioning after the completion of the function?

Just starting out in web programming and currently delving into javascript and jquery ajax.. This snippet represents my javascript code. The refresh variable controls an interval for updating the chat by fetching chats from the database and displaying the ...

What is the process for fetching a texture from a MySql database using three.js?

Is it possible to load a model's texture from a MySql database using three.js and php, based on the logged-in user? My goal is to display the texture on the model that corresponds to the current user. Can I simply use "echo" to retrieve the column con ...

Modify the names of the months (output) in the datetime calendar

Currently, I am developing a web application where I am utilizing a date picker feature from the Materialangular calendar extender. <md-datepicker ng-model="mddate" ng-blur="dateformatechanged()" md-placeholder="Enter date"></md-datepicker> W ...

The range slider initiates with the first alphabet

I have created a range slider with values from 0 to 4, displaying as 0, 1, 2, 3, 4. Currently, the minimum value is set as 0 and the maximum value is set as 4. Now, I am looking to replace 0 with 'F'. For example: F, 1, 2, 3, 4. Is it possible t ...

JSON object fails to iterate with ng-repeat

It must be the scorching temperature... Having a json object that I'm eager to loop through using ng-repeat, it should be straightforward, but alas! it's just not cooperating. Here's the HTML snippet: <a data-ng-repeat="x in template.m ...

Using the scroll feature in the selectyze plugin allows for smooth

The jQuery plugin selectyze from https://github.com/alpixel/Selectyze serves to replace the standard selectbox (dropdown), but there is a small issue that can be quite irritating. I am hoping someone out there may have a solution. Annoying Situation Here ...

Enabling hover effects to scroll divs only when interacted with

I am aiming to achieve two separate scrolling divs and I'm uncertain about the exact approach. Experimenting with various overflow properties has only resulted in one scrolling independently. .profile { width: 100%; display: flex; ...

displaying data once "other" is chosen from a dynamic chart

I am having an issue with a dynamic table where I have a dropdown list with the option "other", and I want to display additional input when "other" is selected. Currently, the function I have only hides the input that is always visible and does not show ...

How to execute an object function in TypeScript only if it is defined

Currently, I am working on creating a binary search tree class in Typescript as part of my learning journey with the language. My aim is to incorporate generics into this exercise. In the process of implementing algorithms within the class, I have encount ...

Listception: A Vertical List Nested Inside a Horizontal List

I am currently working on creating a basic navigation system, and I would like to implement a horizontal list with vertical items within it: Header 1 Header 2 Header 3 -Sub 1 -Sub 1 -Sub 1 -Sub 2 -Sub 2 -Sub 2 -Sub 3 ...

How to resolve the issue of not being able to access functions from inside the timer target function in TypeScript's

I've been attempting to invoke a function from within a timed function called by setInterval(). Here's the snippet of my code: export class SmileyDirective { FillGraphValues() { console.log("The FillGraphValues function works as expect ...

Executing a function once the loading of images in Laravel is completed through an ajax call

I have a fantastic image gallery on my website that operates similar to Pinterest. $images = Images::paginate(5); Initially, my image page displays 5 images. Controller: if($request->ajax()) { return [ 'images' => view(&apos ...

A fresh PHP page escapes from the confines of an iframe and expands to occupy the entire

When calling a new url from inside an iframe, the goal is for the new url to break free from the confines of the iframe and appear full screen. The challenge lies in not having control over the call to the new php file, as it is initiated by a credit card ...

The process of masking a video with alpha data from another video on canvas seems to be experiencing a

I have a unique setup on my page where I'm masking one video with another. Essentially, when the Play button is pressed, a second video slowly appears over the looping video in the background. This effect is achieved by using a black/white mask transf ...

When I try to pass a variable as a prop to another .js file, it mysteriously transforms into an undefined value

Upon successful login, my app file sets the isAuthenticated variable to true and redirects to the /admin page. The Firebase API call is functioning as expected, allowing access only with a valid username and password. The issue arises when I try to hide t ...

Having difficulty understanding and parsing JSON using JQuery

I have a jQuery function that is returning data in the following format: {"Suggestions":[{"name":"RR Restaurant","category_id":"166","locality":"Gayathri Nagar","listing_info":"listing","random_id":"1ll0f0wJ"},{"name":"Oko Restaurant","category_id":"166", ...

What is the reason that the css backdrop-filter: blur() is functioning properly everywhere except on the active bootstrap-4 list-group-item?

I have a gallery with an album-card component inside, and within that is a carousel. I noticed that when I add a list-group and set one of the items as active, it remains unblurred. Can anyone help me understand why? Here is the HTML for the gallery com ...

Arranging a div's position in relation to an unrelated div

I need to position a div element called "a" below another div element known as "b". The HTML code has the following structure: <div id="a"></div> <form> <div id="b"></div> <div id="c"></div> </form> U ...

How about a CSS one-by-one black pixel that's not opaque?

I've been attempting to achieve a 70% transparent black background on an element, but unfortunately, I had to use a pixel to address browser compatibility problems. Using CSS alone would make the entire content of the element transparent as well. Her ...