Javascript "if" statement within alert -
i'm new javascript, , i'm wondering how embed "if" statements within alerts. specifically, i'm working on form, , want alert appears after user clicks "submit" display different messages depending on elements of user's input problematic (if any). know other way around (i.e., use series of if statements determine alert show), hoping able use "if/else" within alert code itself. thanks!
you don't want use alert. it's used exclusively inform user has occurred, , can't feedback it. should use instead prompt or confirm. using confirm code allow determine whether user hit ok or cancel. while limited, still functions in manner similar you're looking for. example
var r=confirm("press button"); if (r==true) { x="you pressed ok!"; } else { x="you pressed cancel!"; } using prompt code allow user input value, can append variable , use logic there, such as
var person=prompt("please enter name","harry potter"); if (person!=null) { x="hello " + person + "! how today?"; document.getelementbyid("demo").innerhtml=x; } the standard syntax prompt function
prompt("this text appear in alert box","this default value"); my source, additional information, available @ w3schools
edit - forgot mention that, if they're using form you've made in javascript, easier run simple if/else statement checks if of values they've input not null , have right datatype before allowing them continue. have else alert, suppose, if you're using if confirm validity, opposed lack thereof.
Comments
Post a Comment