javascript - Using Filter on Radio Buttons -
when using filter on radio button, error stating .filter
not function. far know, selecting radio buttons name returns array , should able use filter on array.
any highly appreciated.
<form action="" name='myform'> <p>select beach</p> <input type="checkbox" value='miami' id='miami' name='check'> <label for="miami">miami</label> <br> <input type="checkbox" value='florida' id='florida' name='check'> <label for="florida">florida</label> <br> <input type="checkbox" value='jamaica' id='jamaica' name='check'> <label for="jamaica">jamaica</label> <p>select airline</p> <input type="radio" value='american' id='american' name='airline'> <label for="american">american</label> <br> <input type="radio" value='air india' id='airindia' name='airline'> <label for="airindia">air india</label> <br> <input type="radio" value='indigo' id='indigo' name='airline'> <label for="indigo">indigo</label> <br> <br> <button type='submit'>submit</button> </form>
script
var myform=document.myform; myform.addeventlistener('submit',getans); function getans(e){ e.preventdefault() var radios=myform.airline, checks=myform.check; console.log(radios); var airline= radios.filter(function(val){ return val.checked; }); console.log(radios); }
it's not array, array-like object. can still use filter, have use call
make work.
var myform=document.myform; myform.addeventlistener('submit',getans); function getans(e){ e.preventdefault() var radios=myform.airline, checks=myform.check; console.log(radios); var airline= [].filter.call(radios, function(val){ return val.checked; }); console.log(airline); }
edit: far clarification, call
allows use methods made other objects or classes, , use them on own objects. in case, filter method made arrays, if use call, can use on form collection. some further documentation here.
Comments
Post a Comment