Show content based on radio option click with Javascript

onclick radio button show hide div javascript
radio button checked show div css
show hide div on click of radio button in angular 7
multiple radio button show/hide div
how to show and hide input fields based on radio button selection
show div on radio button select javascript
radio button javascript onclick=show hide textbox
how to show and hide input fields based on radio button selection in jquery

I have 2 radio buttons.

I would like to show the div when the radio is selected. As default, the default must be selected and the content is shown.

<div class="m-form__group form-group row">
  <div class="col-9">
    <div class="m-radio-inline mt-3">
      <label class="m-radio">
        <input type="radio" name="type" value="1"  checked> First
        <span></span>
      </label>
      <label class="m-radio">
        <input type="radio" name="type" value="2" required=""> Second
        <span></span>
      </label>
    </div>
  </div>
</div>

<div class="first">First Content</div>
<div class="second">Second Content</div>

What is the best way of doing this with Javascript? My code https://jsfiddle.net/cbkm9vxg/

You can add a .hidden class to hide an element, assign it by default and then add/remove is as needed when the radio elements are clicked.

// radio element click event handler
var radioClickHandler = function() {
  var first = document.querySelector('.first');
  var second = document.querySelector('.second');

  // show/hide required elements depending on which radio element was clicked.
  if (this.value == 1) {
    first.classList.remove('hidden');
    second.classList.add('hidden');
  } else {
    first.classList.add('hidden');
    second.classList.remove('hidden');
  }
}

// Get All Radio Elements
var radios = document.querySelectorAll('[type=radio]');

// Apply click event
for (var i = 0; i < radios.length; i++) {
  radios[i].addEventListener('click', radioClickHandler);
}

// Apply Default
radioClickHandler.apply(radios[0]);
/*hides element*/
.hidden {
  display: none;
}
<div class="m-form__group form-group row">
  <div class="col-9">
    <div class="m-radio-inline mt-3">
      <label class="m-radio">
        <input type="radio" name="type" value="1"  checked> First
        <span></span>
      </label>
      <label class="m-radio">
        <input type="radio" name="type" value="2" required=""> Second
        <span></span>
      </label>
    </div>
  </div>
</div>

<div class="first">First Content</div>
<div class="second">Second Content</div>

Show Hide DIV Based on Radio Buttons Selection Using jQuery, How to show and hide div elements based on the selection of radio buttons in jQuery. Topic: JavaScript / jQueryPrev|Next  To get the value of selected radio button, a user-defined function can be created that gets all the radio buttons with the name attribute and finds the radio button selected using the checked property. The checked property returns True if the radio button is selected and False otherwise.

You'll need to add a change event listener on your radioboxes.

For an easier use, add a common class to your text divs (here, text-div).

Inside the event handler, check if the input was checked with this.checked, hide all .text-div divs and only show the one with the index similar to the radiobox value (-1, because it's a 0-indexed array) :

let elements = document.querySelectorAll(".m-radio input");

let myFunction = function() {
  if(this.checked){
    //Get all your text divs
    let els = document.querySelectorAll('.text-div');
    for(let el of els){
      //Hide them all
      el.style.display = 'none';
    }
    //Only show the one with the same index as the radiobox value - 1
    els[(+this.value) - 1].style.display = 'block';
    
  }
};

//Attach your event handler for every radiobox
for (let element of elements) {
    element.addEventListener('change', myFunction, false);
}

//Show the first text div at the start
document.querySelectorAll('.text-div')[0].style.display = 'block';
.text-div{
  display: none;
}
<div class="m-form__group form-group row">
  <div class="col-9">
    <div class="m-radio-inline mt-3">
      <label class="m-radio">
        <input type="radio" name="type" value="1"  checked> First
        <span></span>
      </label>
      <label class="m-radio">
        <input type="radio" name="type" value="2" required=""> Second
        <span></span>
      </label>
    </div>
  </div>
</div>

<div class="text-div first">First Content</div>
<div class="text-div second">Second Content</div>

Very basic toggle show/hide div based on radio button, Very basic toggle show/hide div based on radio button · JavaScript <meta http-​equiv="Content-Type" content="text/html; charset=iso-8859-1" className = '​tab' } hideTabs(); /* hide tabs on load */ /* * Add click events  Use JavaScript to Hide or Show a Portion of a Form - Duration: 6:35. Ralph Phillips 36,946 views

Use this

document.getElementById('firstDiv').style.display = 'block';
document.getElementById('secondDiv').style.display = 'none';

function onChangeRadio() {

  var x = document.getElementById('first').checked;

  if (x) {
    document.getElementById('firstDiv').style.display = 'block';
    document.getElementById('secondDiv').style.display = 'none';
  } else {
    document.getElementById('firstDiv').style.display = 'none';
    document.getElementById('secondDiv').style.display = 'block';
  }

}
<div class="m-form__group form-group row">
  <div class="col-9">
    <div class="m-radio-inline mt-3">
      <label class="m-radio">
        <input onclick="onChangeRadio()" id="first" type="radio" name="type" value="1"  checked> First
        <span></span>
      </label>
      <label class="m-radio">
        <input onclick="onChangeRadio()" id="second" type="radio" name="type" value="2" required=""> Second
        <span></span>
      </label>
    </div>
  </div>
</div>

<div id="firstDiv" class="first">First Content</div>
<div id="secondDiv" class="second">Second Content</div>

How to Show and Hide div elements using radio buttons , How to Create Image Lightbox Gallery using HTML CSS and JavaScript ? name for radio button is same as the element which is used to display the content​. to hide or show one record from an ng-repeat within a table based on ng-click​? Topic: JavaScript / jQuery Prev|Next. Answer: Use the jQuery show() and hide() methods. You can simply use the jQuery show() and hide() methods to show and hide the div elements based on the selection of radio buttons. The div boxes in the following example are hidden by default using the CSS display property which value is set to none.

This is fairly Simple so i'll go with a simple answer.

If you couldn't figure this on your own, then this should be as simple for you to try and understand.

function onClick(e){
	if (e.target.value == 1) 
	{
		document.querySelector('.first').style.display='block';
		document.querySelector('.second').style.display='none';
	}
	else if (e.target.value == 2)
	{
		document.querySelector('.first').style.display='none';
		document.querySelector('.second').style.display='block';
	}
}
<div class="m-form__group form-group row">
  <div class="col-9">
    <div class="m-radio-inline mt-3">
      <label class="m-radio">
        <input type="radio" onclick="onClick(event);" name="type" value="1"  checked> First
        <span></span>
      </label>
      <label class="m-radio">
        <input type="radio" onclick="onClick(event);" name="type" value="2" required=""> Second
        <span></span>
      </label>
    </div>
  </div>
</div>

<div  class="first">First Content</div>
<div style="display:none;" class="second">Second Content</div>

Exposing Additional Form Fields via Checked Radio Buttons, Skip to main content Exposing Additional Form Fields via Checked Radio Buttons bindUIActions: function() { // when a radio or checkbox changes value, click or We could do the hide/showing through JavaScript as well, but CSS is quite well Not directly in Safari, but in apps that use the built in WebView to show the  Login Form Signup Form Checkout Form Contact Form Social Login Form Register Form Form with Icons Newsletter Stacked Form Responsive Form Popup Form Inline Form Clear Input Field Hide Number Arrows Copy Text to Clipboard Animated Search Search Button Fullscreen Search Input Field in Navbar Login Form in Navbar Custom Checkbox/Radio Custom Select Toggle Switch Check Checkbox Detect Caps Lock Trigger Button on Enter Password Validation Toggle Password Visibility Multiple Step Form Autocomplete

(function() {
  document.querySelector('.second').style.display = 'none';

  document.querySelector('.m-radio-inline').addEventListener('click', function(event) {
    let traget = event.target.querySelector('input');
    let targetValue = traget && traget.value;
    if (targetValue) {
      document.querySelector(`.${targetValue}`).style.display = 'block';
      document.querySelector(`.${targetValue == 'second'?'first': 'second'}`).style.display = 'none';
    }
  });
})();

HTML DOM Input Radio value Property, For radio buttons, the contents of the value property do not appear in the user together with a text input field to display the value of the selected radio button:. Setting Field Visibility based on a Radio Button Setting. One characteristic of Radio Buttons is that the user can only turn a button on. The buttons are turned off automatically when the user makes another selection. This characteristic simplifies the code for showing and hiding fields in Sample #2. In this sample there are two Radio Buttons

JavaScript Examples, If you would like to display a form based on a single checkbox This example uses a text field (Key 6j2syb) and a radio button field (ID 456). It would be better to have the "Hide" radio button marked as checked in the HTML, then let the JavaScript follow suit. To bind an event handler and execute it right away, you could use .trigger() , but note that triggering the 'click' event also toggles the radio state, so you'll want to make a separate 'init-post-format' event type.

<input type="radio">, Only one radio button in a given group can be selected at the same time. to the push buttons on old-fashioned radios, such as the one shown below. Then we add some JavaScript to set up an event listener on the submit that you can select a radio button by clicking on its associated <label> element,  How do I use Radio Buttons to switch content inside a document Hi, I want to use radio buttons in MS Word 2013 to switch between contents, i.e. selecting one radio button will show some content, and selection the other will show something different.

:checked, The :checked CSS pseudo-class selector represents any radio (input type="radio​"), checkbox (input type="checkbox"), or option (option in a to let the user toggle content based on the state of a checkbox, all without using JavaScript. border: 1px solid; border-radius: 3px; } /* Show hidden content when  Click on a <button> element to display the current day, date and time: <button onclick="getElementById('demo').innerHTML = Date()">What is the time?</button> Try it Yourself »

Comments
  • Use the change event and show/hide your content? What have you tried so far?
  • document.getElementById('first').style.visibility = 'visible'; document.getElementById('second').style.visibility = 'hidden';
  • my only problem is snce I have other radio button in my page, other radio button's first and last also effects the content. How can I fix this issue
  • There si many ways to solve that, you could give the buttons you want to attach the events to specific attributes, such as data-id="xxx" then use document.querySelector('[data-id="xxx"]') to bind to them.
  • if you could add your preffered method to the code snippet I would appricate that. Thank you
  • I'm guessing, I have no idea what the rest of your page looks like. If you have multiple groups of radio buttons and only want to apply the logic to a specific subset you need to add those specific scenarios to a new question as it is different to the current question and none of the current solution would apply.
  • the "Second Content" class has same name as first content. The first time I posted I mistyped and update. It will be <div class="second">Second Content</div> Would it change your code? Because now you have for etc.
  • @SNaRe You'll have to give a common class to all your divs, and use this class in the querySelectorAll.
  • OP never said anything about jQuery.
  • Yet not what OP was asking.