javascript - Finding out if a form element is of type select using its name attribute -
<form name="form1" method="post" action="survey.php"> <p>q2: best best friend?</p> <select name='q2' id='q21'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <select name='q2' id='q22'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <p>q3: how rate abdelhai ebaid?</p> <p> <input type='radio' name='q3' value='1' >1</p> <p> <input type='radio' name='q3' value='2' >2</p> <p> <input type='radio' name='q3' value='3' >3</p> <p> <input type='radio' name='q3' value='4' >4</p> <p> <input type='radio' name='q3' value='5' >5</p><span style='color:red' id='radio_error'></span> <p>q4: best best friend?</p> <select name='q4' id='q41'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <select name='q4' id='q42'> <option value='0'>select name</option> <option value='2001'>ahmed ebaid</option> <option value='2002'>jaida eltayeby</option> <option value='2003'>farida ebaid</option> <option value='2005'>kenny andersen</option> <option value='2006'>nadine saad</option> <option value='2007'>abdelhai ebaid</option> <option value='2008'>salwa abdelaal</option> </select> <p> <input type="submit" name="submit1" value="click here vote"> </p> </form> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <script> if ($('#'+qnum+'').is(':radio')) { if (($('input[name='+qnum+']:checked').length == 0) ){ alert("no selection made "+ qnum); return false; } } else if ($('[name="' + qnum + '"]').is(':radio')) { var j = 1; var nominatefriend = qnum+j; alert (nominatefriend); } </script
what i'm trying here validate form elements based on types, javascript code, included loop goes through questions. don't know right syntax of checking whether form element select or not based on name attribute. qnum @ case variable takes values such q1, q2,..... alert(nominatefriend) never gets executed
you can achieve in various ways,
var element = $('#'+qnum);
you can using jquery:
if (element.is('input')) { // } else if (element.is('select')) { // }
or
if (element.tagname == "input") { // } else if (element.tagname == "select") { // }
or
if(element.prop('type') == 'select-one' ) { // }
Comments
Post a Comment