Xoog
Xoog

Reputation: 926

Setting a checkbox to unchecked when clicking on drop down

I'd like to be able to uncheck a checkbox if my drop down has been selected using jQuery. I feel like I might almost be there but can't figure out why it isn't working. For example, I click MR but then I change to doctor - MR stays selected.

<script>
$('.multiple-choice').click(function(){
$('input').prop('checked', false);
});
</script>

<fieldset  class="title-box">

<span><input type="radio" name="title" value="Mr" onclick="titlehandler(this)" id="titlemr" th:field="*{title}" /> <label for="titlemr" id="mr-label">Mr</label></span>
<span><input type="radio" name="title" value="Mrs" onclick="titlehandler(this)" id="titlemrs" th:field="*{title}" /> <label for="titlemrs" id="mrs-label">Mrs</label></span>
<span><input type="radio" name="title" value="Miss" onclick="titlehandler(this)" id="titlemiss" th:field="*{title}" /> <label for="titlemiss" id="miss-label">Miss</label></span>
<span><input type="radio" name="title" value="Ms" onclick="titlehandler(this)" id="titlems" th:field="*{title}" /> <label for="titlems" id="ms-label">Ms</label></span>

    <span><label for="dropdown">Other</label>
    <select name="dropdown" id="dropdown" class="multiple-choice"> 


<option value="Dr" name="othertitle1" id="othertitle1">Dr</option>
<option value="Rev">Rev</option>
<option value="Sir">Sir</option>
<option value="Sist">Sist</option></select>
</fieldset>

<script>
	$('.multiple-choice').click(function(){
    $('input').prop('checked', false);
    });
    </script>
    <fieldset  class="title-box">
							
	<span><input type="radio" name="title" value="Mr" onclick="titlehandler(this)" id="titlemr" th:field="*{title}" /> <label for="titlemr" id="mr-label">Mr</label></span>
	<span><input type="radio" name="title" value="Mrs" onclick="titlehandler(this)" id="titlemrs" th:field="*{title}" /> <label for="titlemrs" id="mrs-label">Mrs</label></span>
	<span><input type="radio" name="title" value="Miss" onclick="titlehandler(this)" id="titlemiss" th:field="*{title}" /> <label for="titlemiss" id="miss-label">Miss</label></span>
	<span><input type="radio" name="title" value="Ms" onclick="titlehandler(this)" id="titlems" th:field="*{title}" /> <label for="titlems" id="ms-label">Ms</label></span>

		<span><label for="dropdown">Other</label>
		<select name="dropdown" id="dropdown" class="multiple-choice"> 
                           

    <option value="Dr" name="othertitle1" id="othertitle1">Dr</option>
    <option value="Rev">Rev</option>
    <option value="Sir">Sir</option>
    <option value="Sist">Sist</option></select>
    </fieldset>

Upvotes: 1

Views: 68

Answers (3)

Milan Chheda
Milan Chheda

Reputation: 8249

I removed onclick="titlehandler(this)" and it seems to be working fine:

$('.multiple-choice').click(function() {
  $('input').prop('checked', false);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset class="title-box">

  <span><input type="radio" name="title" value="Mr" id="titlemr" th:field="*{title}" /> <label for="titlemr" id="mr-label">Mr</label></span>
  <span><input type="radio" name="title" value="Mrs"  id="titlemrs" th:field="*{title}" /> <label for="titlemrs" id="mrs-label">Mrs</label></span>
  <span><input type="radio" name="title" value="Miss"  id="titlemiss" th:field="*{title}" /> <label for="titlemiss" id="miss-label">Miss</label></span>
  <span><input type="radio" name="title" value="Ms"  id="titlems" th:field="*{title}" /> <label for="titlems" id="ms-label">Ms</label></span>

  <span><label for="dropdown">Other</label>
    <select name="dropdown" id="dropdown" class="multiple-choice"> 


<option value="Dr" name="othertitle1" id="othertitle1">Dr</option>
<option value="Rev">Rev</option>
<option value="Sir">Sir</option>
<option value="Sist">Sist</option></select>
</fieldset>

Upvotes: 1

Difster
Difster

Reputation: 3270

Either put this part at the bottom of your file or wrap it in a $(document).ready() callback.

<script>
$('.multiple-choice').click(function(){
$('input').prop('checked', false);
});
</script>

Upvotes: 1

Chandra Kumar
Chandra Kumar

Reputation: 4205

Try this code:

<script>
$('.multiple-choice').click(function(){
    $("input[type='radio'").each(function () {
        $(this).prop('checked', false);
    });
});
</script>

Upvotes: 1

Related Questions