bfmr89
bfmr89

Reputation: 77

check radio button on click input text (form)

I'm trying to change a radio button to checked when a input text is clicked in one FORM.
Here is my form:

<input type="radio" name="sel_limit" id="sel_limit_1" value="1" <?php if($limit_days) echo 'checked'?>> <?php echo MOD_CO_DAYS; ?> <input type="text" class="inputbox" name="limit_1" id="limit_1" value="<?php echo $limit_days; ?>" size="5"/>&nbsp;
<input type="radio" name="sel_limit" id="sel_limit_2" value="2" <?php if($limit_weeks) echo 'checked'?>> <?php echo MOD_CO_WEEKS; ?> <input type="text" class="inputbox" name="limit_2" id="limit_2" value="<?php echo $limit_weeks; ?>" size="5"/>

Here js code:

$('#limit_1').click(function(){
    $('#limit_2').val('0');
    $('#sel_limit_1').attr('checked', true);
});

$('#limit_2').click(function(){
    $('#limit_1').val('0');
    $('#sel_limit_2').attr('checked', true);
});

I tried to use attr and prop with and without true, nothing happens.
This is not working because is one form?
I'm using jquery 1.8.3

Upvotes: 0

Views: 935

Answers (2)

Rituraj ratan
Rituraj ratan

Reputation: 10378

by attr use checked

ALL the DOWNVOTER SEE THIS DEMO AND SEE ITS WORKS DEMO

$(document).ready(function(){
$('#limit_1').click(function(){
     $("input[type='text'").val('');
     $("input[type='radio'").removeAttr("checked"); 
    $('#limit_2').val('0');
    $('#sel_limit_1').attr('checked', 'checked');
});

$('#limit_2').click(function(){
   $("input[type='text'").val('');
   $("input[type='radio'").removeAttr("checked");      
   $('#limit_1').val('0');
    $('#sel_limit_2').attr('checked', 'checked');
 });
});

by prop use true

$(document).ready(function(){
$('#limit_1').click(function(){
      $("input[type='text'").val('');
      $("input[type='radio'").prop("checked",false);     
      $('#limit_2').val('0');
      $('#sel_limit_1').prop('checked', true);
});

$('#limit_2').click(function(){
      $("input[type='text'").val('');
      $("input[type='radio'").prop("checked",false);        
      $('#limit_1').val('0');
      $('#sel_limit_2').prop('checked', true);
});
});

Upvotes: -1

neel shah
neel shah

Reputation: 2271

Use the new .prop() function:

  $('#sel_limit_2').prop('checked', true);

jQuery 1.5 and below

The .prop() function is not available, so you need to use .attr().

  $('.myCheckbox').attr('checked','checked');

Upvotes: 2

Related Questions