Monday, May 20, 2024
31
rated 0 times [  38] [ 7]  / answers: 1 / hits: 16362  / 12 Years ago, thu, february 21, 2013, 12:00:00

I tried to do an if/then statement in javascript but the then command 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();
}

More From » if-statement

 Answers
84

I'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?


[#80077] Wednesday, February 20, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
minab

Total Points: 701
Total Questions: 104
Total Answers: 91

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;