if then statement javascript - Stack Overflow
I tried to do an "if/then" statement in javascript but the "then" mand is being ignored. Any ideas on why?
Specifically I want the form to be validated that the two text boxes are not blank and once that is validated I want a DIV ID = section2 to appear.
function checkanswers() {
var fdet = document.wastheform;
if (fdet.question_type[0].checked) {
var elements = new Array("name","address");
var elements_name = new Array("name", "address");
for (var i = 0; i < elements.length; i++) {
if ($("#" + elements[i]).val() == "") {
err = "Please enter " + elements_name[i] + "";
alert(err);
return false;
}
}
$("#section2").show();
}
I tried to do an "if/then" statement in javascript but the "then" mand is being ignored. Any ideas on why?
Specifically I want the form to be validated that the two text boxes are not blank and once that is validated I want a DIV ID = section2 to appear.
function checkanswers() {
var fdet = document.wastheform;
if (fdet.question_type[0].checked) {
var elements = new Array("name","address");
var elements_name = new Array("name", "address");
for (var i = 0; i < elements.length; i++) {
if ($("#" + elements[i]).val() == "") {
err = "Please enter " + elements_name[i] + "";
alert(err);
return false;
}
}
$("#section2").show();
}
Share
Improve this question
edited Feb 21, 2013 at 16:01
Konstantin Dinev
35k14 gold badges79 silver badges102 bronze badges
asked Feb 21, 2013 at 16:00
John MontagueJohn Montague
951 gold badge5 silver badges15 bronze badges
3
-
You have two
if
here. Which one doesn't seem to work ? And how did you chek it ? – Denys Séguret Commented Feb 21, 2013 at 16:01 - Aren't you confusing names and ids ? – Denys Séguret Commented Feb 21, 2013 at 16:02
- 1 set a break point on the if statements and then use "add watch" to evaluate the different parts of the expression inside the if statement to determine why the left side doesn't equal the right side – Brandon Commented Feb 21, 2013 at 16:04
2 Answers
Reset to default 0I'm assuming your "then" is the showing of the div. In which case I would do something like this:
function checkanswers() {
var fdet = document.wastheform;
if (fdet.question_type[0].checked) {
if(withoutValue("#name")) {
alert("Please enter name")
}
else if(withoutValue("#address")) {
alert("Please enter address")
} else {
$("#section2").show();
}
}
}
function withoutValue(selector) {
return $(selector).val() === "";
}
Is that what you're looking for?
Your question implies that you want an if/then behavior (in Javascript, this is more properly referred to as an if/else statement), but the way you structured your code, there is no "then". Here is how the if statement should work:
if ( condition )
{
// do something if the condition is true
}
else
{
// do something if the condition is false
}
- 谷歌跟微软干上了:公布更多Windows安全漏洞
- 苹果安卓交锋:开放做强生态链
- 苹果兜售没用软件脸不红心不跳
- 英特尔公布搭载其芯片的Windows 8平板电脑的硬件配置规范
- visual studio code - Azure Functions HTTPTrigger running locally with VSCode weird bug but API still works .NET - Stack Overflow
- sirishortcuts - How to Set Custom Icon for iOS App Shortcut? - Stack Overflow
- for loop - CSS masonry grid with dynamic rows width - Stack Overflow
- python - Plotting a list of integer 3d values into Axes3d.voxels - Stack Overflow
- javascript - Is there a way to get the difference between 2 points by click and hold with the mouse on a line on chart.js? - Sta
- c++ - Access violation on ending the main function scope but only with Visual Studio 2019 and gcc - Stack Overflow
- OpenAI Assistant: File Search tool stops working when I enable custom function calling - Stack Overflow
- You don’t have permission to view or edit anything. Django Admin. Web-site for school - Stack Overflow
- node.js - Electron app with CRA failing child process creation - Stack Overflow
- regex - Change text block with nearest search pattern - Stack Overflow
- reactjs - How to deploy Laravel (with react) app on heroku without any error (like 419 error) - Stack Overflow
- Swift Predicate: Fatal Error Keypaths With Multiple Components - Stack Overflow
- python - Call function from macos framework (e.g. IntelPowerGadget) - Stack Overflow