I
I
its2easyy2015-11-08 13:51:38
JavaScript
its2easyy, 2015-11-08 13:51:38

How to hang up handlers on input radio change?

I have a form that contains two radio inputs and an inactive text input

<form name="form1" id="form1">
   ...
   <label><input type="radio" name="radio1" value="0" checked/>Нет</label>
   <label><input type="radio" name="radio1" value="1"/>Да</label>
  
   <input type="text" value="100" name="input1"  disabled/>
   ...
</form>

It is necessary that when switching radio, the text field becomes available, and when switching back, it turns off again. Clicking on the label should also work.
Should I use the click or change event? And is it possible to hang a handler on the radio array without using a loop?
Preferably in native js.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Inchin ☢, 2015-11-08
@its2easyy

form1.onchange = function(e){
      if(e.target.name == "radio1"){
         this.elements.input1.disabled = !+e.target.value;
      }
}

jsfiddle.net/8pxxh4ux
Similar to jQuery:
$("#form1").on("change", "input[name=radio1]", function(){
    var $this = $(this);
    $this.next("input[name=input1]").prop("disabled", !+$this.val());
    //this.form.elements.input1.disabled = !+this.value;
});

A
Alexey Ukolov, 2015-11-08
@alexey-m-ukolov

Should I use the click or change event?

Change - you can change the value without clicking.
And is it possible to hang a handler on the radio array without using a loop?
Only if you use jquery (and then, there will still be a cycle inside).

L
lnked, 2015-11-08
@lnked

$('input[type="radio"]').on('change', function(e){
 //состояние $(this).prop('checked');
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question