Erez Savir
Erez Savir

Reputation: 74

jQuery - radio button on click and not on change

I'm trying to find a solution to an issue I have between click and change. I need to capture the click event and not change. I'll explain the situation:

I have a radio button list with 3 items. Each click I need to clean a div. However, If i'm posting back and return to the client with an error(submit, server validation check failed), the change event is fired once again (obviously), and the div is cleaned.

Is there a way to distinguish between click and the checked state?

I hope I have made myself clear. Edit: Added some code:

$("input[name*='SelectedOwner']").on('change',
function () {
    var radioId = $(this).val();
    if (radioId === "2" || radioId === "3") {
        $("#div1").hide();
        $("#divclean :input").removeAttr("disabled");
    } else {
        $("#div1").show();
        $("#divclean :input").attr("disabled", true);
    }
});
$("input[name*='SelectedOwner']").on('click', function () {
   //Clean the output at each change
   $("#divclean :input").val("");
});

Thanks.

Upvotes: 0

Views: 1858

Answers (2)

Laurianti
Laurianti

Reputation: 975

	$('input[name="choose"]').click(function(e) {
			if ($(this).data('clicked')) {
				$('#theDiv').text('You have REclicked "'+ $(this).val() + '" value');
			}
			else {
				$('input[name="choose"]').data('clicked', false);
				$(this).data('clicked', true);
				$('#theDiv').text('First time you click "'+ $(this).val() + '" value');
			}
	})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  
  <div id="theDiv"></div>
  
  A
  <input type="radio" name="choose" value="a" />
  
  B
  <input type="radio" name="choose" value="b" />
  
  C
  <input type="radio" name="choose" value="c" />

Upvotes: 1

ℛɑƒ&#230;Ŀᴿᴹᴿ
ℛɑƒ&#230;Ŀᴿᴹᴿ

Reputation: 5326

You should to bind the change event to every radio button:

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

With jQuery is also possible:

if ($("#r1").is(":checked")) {}

More informations: JQuery $(#radioButton).change(...) not firing during de-selection


I hope this helps...

Good Luck!

Upvotes: 0

Related Questions