jquery - Clearing a textbox field when a radio button is selected in Javascript -
i have web form allows users donate money using predefined radio buttons value assigned them (all different numbers). have choose own amount textfield can write in custom amount wish donate. want clear custom textfield if user selects predefined choice.
so far have created this:
html:
<input type="radio" name="am_payment" value="3" checked="checked"> <strong>64</strong> <input type="radio" name="am_payment" value="11" checked="checked"> <strong>100</strong> <input type="radio" name="am_payment" value="32" checked="checked"> <strong>250</strong> <input type="radio" value="" name="am_payment"><label>other</label> <input type="text" name="cp_otheramount" value="" id="theamount" disabled="disabled"/>
javascript:
$('input[name="am_payment"]').on('click', function() { if ($(this).val() === '') { $('#theamount').removeprop("disabled"); } else { $('#theamount').prop("disabled", "disabled"); $('input[name="cp_otheramount"]').val(""); } });
but basing on value === true
value=""
doesn't seem right.
is there way improve this?
thanks
to disable/enable element need set value of disabled property true/false, removing property doesn't work need
$('input[name="am_payment"]').on('click', function() { if ($(this).val() === '') { $('#theamount').prop('disabled', false); } else { $('#theamount').prop("disabled", true).val(''); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="radio" name="am_payment" value="3" checked="checked"> <strong>64</strong> <input type="radio" name="am_payment" value="11" checked="checked"> <strong>100</strong> <input type="radio" name="am_payment" value="32" checked="checked"> <strong>250</strong> <input type="radio" value="" name="am_payment"> <label>other</label> <input type="text" name="cp_otheramount" value="" id="theamount" disabled="disabled" />
Comments
Post a Comment