Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display hidden content when radio button is checked using JQuery

Tags:

html

jquery

css

I have two check-boxes

ABC
XYZ

when ABC is checked i'm showing abc content div and when XYZ is checked i'm showing xyz content div

everything works great only when i click on ABC and XYZ radio buttons suppose if by default ABC radio button is checked abc content div is not displaying only on clicking i'm getting content of it

my requirement is when radio button is checked i should get hidden content of that particular checked radio button not on clicking

can somebody help me out in achieving it

http://jsfiddle.net/Qac6J/497/

HTML

<form id='group'>
    <div>

        <label>
            <input type="radio" name="group1" class="trigger" data-rel="abc" checked />ABC
        </label>

        <span class="abc content">
            <label>
                <span>I belong to ABC</span>
                <input type="button" value="ABC"/>
            </label>
        </span>

    </div>
    <br>
    <div>

        <label>
            <input type="radio" name="group1" class="trigger" data-rel="xyz"/>XYZ
        </label>

        <span class="xyz content">
            <label>
                <span>I belong to XYZ</span>
                <input type="button" value="XYZ"/>
            </label>
        </span>

    </div>
</form>

CSS

.content
{
    display: none;
}

JQuery

$('.trigger').change(function() 
{
    $('.content').hide();
    $('.' + $(this).data('rel')).show();
});
like image 880
Sjay Avatar asked Nov 06 '15 05:11

Sjay


People also ask

How to show/hide Div on radio button selected using jQuery?

Use jQuery show () and hide () to Show/Hide div on radio button selected. To do this, you need to add some style, script, and few HTML. Hide each div’s which you want to display on select of the radio buttons. The dynamic changes happen based on the click of the selected radio button selection.

How to display data/content of a specific element by selecting radio button?

In order to display data/content of a specific element by selecting the particular radio button in jQuery we can use the following two methods: hide () methods: This method is used to hiding the syntax or the element of html that you want to hide.

How to show or hide the textbox using JavaScript?

Here, function handleClick (myRadio) is a method or function which can be used for calling the value. After that by using “If else” condition, we are showing or hiding the textbox by using JavaScript. So that it can be helpful to hide the text for other options and to show for validity option.

What is the use of show () and hide () methods in HTML?

show () methods: This method is used to show the syntax or the element of html that you want the user to see. content. Use show () method for displaying the element, otherwise use hide () method for hiding.


2 Answers

You need to get the rel value of checked radio thus use :checked selector

$('.trigger').change(function () {
    $('.content').hide();
    $('.' + $('.trigger:checked').data('rel')).show();
}).change(); //Show content on page load

Fiddle

Thanks @tushar With some structure change you can achieve it using pure CSS.

You can use :checked to select the checked radio button and sibling selector + to select the next span and input siblings and use display: block; on them to show.

.content {
  display: none;
}
input:checked + span,
input:checked + input {
  display: block;
}
<form id='group'>
  <div>
    <label>
      <input type="radio" name="group1" class="trigger" data-rel="abc" checked />ABC
      <span class="abc content">
        <span>I belong to ABC</span>
      <input type="button" value="ABC" />
      </span>
    </label>
  </div>
  <br>
  <div>
    <label>
      <input type="radio" name="group1" class="trigger" data-rel="xyz" />XYZ
      <span class="xyz content">
        <span>I belong to XYZ</span>
      <input type="button" value="XYZ" />
      </span>
    </label>
  </div>
</form>
like image 100
Satpal Avatar answered Sep 29 '22 11:09

Satpal


just Remove .change(); from your JS

$('.trigger').change(function () {
    $('.content').hide();
    $('.' + $('.trigger:checked').data('rel')).show();
});

Your updated Fiddle

like image 22
Gaurang Joshi Avatar answered Sep 29 '22 12:09

Gaurang Joshi