html - Javascript catch unselected radio button along with textboxes and alert -
i'm practically new here , i'm beginner in programming. creating html/js based template team easy consolidation of data , copy clipboard can paste in our main tool.
the problem doesn't seem work (at least here @ office, @ home work). doesn't prompt when radio selection empty, resorting using current function catches textboxes/textarea empty. (sample code below)
if (document.getelementbyid('inbrief').value == "") { errcatch +="-issue/request \n"; valid = false; } if (document.getelementbyid('indesc').value == "") { errcatch +="-issue/request description \n"; valid = false; } if (!valid) { document.body.removechild(dummytxtarea); alert(errcatch); return valid; } else { document.body.removechild(dummytxtarea); alert ("data has been copied clipboard."); }
the above if else inside function called when evenlistener triggered via "click" of submit button. tried inserting 'for' statement above if else inside function wont work , alert show textbox/area empty. in advance help
your code throwing errors @ lines 4 , 5 when trying value of unchecked radio box , set variable value:
var selectedlob = document.queryselector('input[name="inlob"]:checked').value; //lob selected var selectedsev = document.queryselector('input[name="insev"]:checked').value; //severity selected
in order fix must first check if radio selected, , if value selected. can replaceing lines 4 , 5 following:
var selectedlob = document.queryselector('input[name="inlob"]:checked') ? document.queryselector('input[name="inlob"]:checked').value :false; var selectedsev = document.queryselector('input[name="insev"]:checked') ? document.queryselector('input[name="insev"]:checked').value :false;
this code first check see if radio checked, if set variable value, if not set variable false. (the a ? b : c
know conditional or ternary operator , short if()
else()
statement). change stop errors being thrown.
you need update checks further down radios to:
if (selectedlob == false) { errcatch +="-choose lob \n"; valid = false; } if (selectedsev == false) { errcatch +="-choose severity \n"; valid = false; }
as side note don't have set uncheck radios false, set them string 'unchecked' or ''empty' if helps make code more readable. sure make sure checks match set to.
Comments
Post a Comment