Himanshu Bhardiya
Himanshu Bhardiya

Reputation: 1057

jquery radio button click and change function not working

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?

Upvotes: 0

Views: 188

Answers (3)

user5077709
user5077709

Reputation:

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

$("#r1, #r2, #r3").change(function () {

Upvotes: 2

Anwar
Anwar

Reputation: 4246

$(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.

Upvotes: 0

Ahs N
Ahs N

Reputation: 8366

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.

Upvotes: 0

Related Questions