0

I want to hide and show div according to radio buttons value. HTML code is,

<div id="share_to_others">
  <input type="radio" value="33" id="fx_sharepl_type" name="fx_sharepl_type">
  <input type="radio" value="22" id="fx_sharepl_type" name="fx_sharepl_type">
  <input type="radio" value="11" id="fx_sharepl_type" name="fx_sharepl_type">
</div>

And jquery code that i tried is,

$("#share_to_others input[name='fx_sharepl_type']").click(function () {
    alert("test");
});

$("#share_to_others input[name='fx_sharepl_type']").change(function () {
    alert("test");
});

$("#fx_sharepl_type").change(function () {
    alert("asdas");
});

$("input[name=fx_sharepl_type]:radio").change(function () {
    alert("click fired");   
});

$(document).on('change', 'input:radio[name=fx_sharepl_type"]', function (event) {
    alert("click fired");
});

Many of them from jsfiddle working demo, But not working for me, i dont know why. Am i doing anything wrong?

8
  • 3
    where is the div html?? also IDs should be unique. Commented Jul 3, 2015 at 12:06
  • do you have a fiddle for yourself ? Commented Jul 3, 2015 at 12:07
  • no i used fiddle from other question and its working, but the same code not working for me Commented Jul 3, 2015 at 12:08
  • </div> instead of <?div> and id (s) must be unique. in any case, it works: jsfiddle.net/qryz8b6o Commented Jul 3, 2015 at 12:09
  • Are you actually loading JQuery? Are you running this code after the elements are in the DOM? Commented Jul 3, 2015 at 12:10

3 Answers 3

2

You have to give unique id to each radio button then after do like this way.

$("#r1, #r2, #r3").change(function () {
Sign up to request clarification or add additional context in comments.

2 Comments

but i have dynamic radio buttons, so how can i define all button in change function
@ Sanket. ok then also you can give separate id to each radio button via php or Jquery.
0

$(function() { // DOM loaded event handler
  var show_duration = 0;
  var content_33 = $("#content_33");
  var content_22 = $("#content_22");
  var content_11 = $("#content_11");
  var bloc_radio_share = $("#share_to_others");
  
  // Take an html element in parameter and show it
  function show_content(content_id) {
    content_id.show(show_duration); 
  }
  
  // Take an html element in parameter and hide it
  function hide_content(content_id) {
    content_id.hide(0); 
  }
  
  hide_content(content_22);
  hide_content(content_11);
  
  
  bloc_radio_share.change(function() {
    var radio_checked_val = $('input[name=fx_sharepl_type]:checked', '#share_to_others').val();
    
    if (radio_checked_val == 33) {
      hide_content(content_22);
      hide_content(content_11);
      show_content(content_33);
    }
    else if (radio_checked_val == 22) {
      hide_content(content_33);
      hide_content(content_11);
      show_content(content_22);
    }
    else { // case content == 11
      hide_content(content_33);
      hide_content(content_22);
      show_content(content_11);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="share_to_others">
  <label>
    <input type="radio" value="33" id="fx_sharepl_type" name="fx_sharepl_type" checked />
    33
   </label>
  <label>
    <input type="radio" value="22" id="fx_sharepl_type" name="fx_sharepl_type" />
    22
  </label>
  <label>
    <input type="radio" value="11" id="fx_sharepl_type" name="fx_sharepl_type" />
    11
  </label>
</div>
<div id="content_33">
  This div is displayed because of click on radio button 33
</div>
<div id="content_22">
  Radio button 22 has been clicked so I appear
</div>
<div id="content_11">
  If you click on radio button 11 you'll see me, like now
</div>

Here is an example of algorithm that fits your need. Logic may be not the best or the fastest but that is a begin.

Comments

0

enter image description hereYour code wors but you forgot to include a JQuery source version located to the left side of the JSFiddle window. Selecting any version of JQuery will make your code work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.