One way to control the visibility of a div using radio button selection without using the onclick method in JavaScript is by using the addEventListener() method to attach a listener to each radio button.
Here's an example code:
<!-- HTML -->
<input type="radio" name="visibility" value="visible"> Show
<input type="radio" name="visibility" value="hidden"> Hide
<div id="myDiv">This is the content of the div</div>
// JavaScript
const showRadioButton = document.querySelector('input[value="visible"]');
const hideRadioButton = document.querySelector('input[value="hidden"]');
const myDiv = document.getElementById('myDiv');
showRadioButton.addEventListener('change', function() {
myDiv.style.display = 'block';
});
hideRadioButton.addEventListener('change', function() {
myDiv.style.display = 'none';
});
In this code, we first select the radio buttons and the div using their unique IDs and value attributes. Then, we attach a change event listener to each radio button using the addEventListener() method. When the user selects a radio button, it triggers the corresponding event listener function that sets the display property of the div accordingly. If the "Show" radio button is selected, the div is displayed by setting its display property to "block". If the "Hide" radio button is selected, the div is hidden by setting its display property to "none".
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-07-10 09:26:55 +0000
Seen: 10 times
Last updated: Jul 10 '23
What is the process for generating a dynamic subdomain/URL using vue.js?
How can the style of the loader be modified while the form submission is being processed?
I'm attempting to develop a Javascript-based comments section for my website.
What are some feasible methods to enable MIDI file playback on a web browser?
How can I resolve the issue of being unable to use Fetch to POST an array of Selected Checkboxes?
What is the method to hide the scroll button when reaching the bottom?
What is the process of using a custom nunjucks filter to filter collections in an eleventy template?
What is the method to create a dynamic checkbox using API data?
How can the user id from the login object be utilized in the resolvers after logging in?