2

HTML

<div class="btn-group" data-toggle="buttons">
            <label class="btn btn-default active">
                <input type="radio" name="payment_options" value=1 autocomplete="off" checked>Cash on Delivery
            </label>
            <label class="btn btn-default" id="bkash-radio">
                <input type="radio" name="payment_options" value=2 autocomplete="off">bKash
            </label>
        </div>

        <input type="hidden" name="trx_id" id="trx_id" class="form-control" placeholder="Enter Transaction ID"/>

jQuery function:

$(document).on("click", "#bkash-radio", function() {
    console.log('test');
    $("#trx_id").attr("type", "text");
});

$(document).on("blur", "#bkash-radio", function() {
    console.log('test');
    $("#trx_id").attr("type", "hidden");
});

I am trying to show the the textbox id=trx_id when the second radio option is selected, but when it is deselected I want the text box to be hidden. How can I do this?

Nayantara Jeyaraj
  • 2,406
  • 5
  • 30
  • 55
MiniGunnR
  • 4,590
  • 5
  • 33
  • 54

1 Answers1

2
  1. Target your [name='payment_options'] radios
  2. On change event see if this.value==="2"
  3. Than manipulate the prop type respectively

$(document).on("change", "[name='payment_options']", function() {
  $("#trx_id").prop("type", this.value==="2" ? "text" : "hidden");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-default active">
    <input type="radio" name="payment_options" value=1 autocomplete="off" checked>Cash on Delivery
  </label>
  <label class="btn btn-default" id="bkash-radio">
    <input type="radio" name="payment_options" value=2 autocomplete="off">bKash
  </label>
</div>

<input type="hidden" name="trx_id" id="trx_id" class="form-control" placeholder="Enter Transaction ID"/>
Roko C. Buljan
  • 164,703
  • 32
  • 260
  • 278