-1
var costp;
var anzahl_der_personen = $('#anzahl_der_personen').val();

    if( +anzahl_der_personen == 1 ) {
        costp = 0;
    }elseif( $.trim('#anzahl_der_personen').val() === null ){
        costp = 0;
    }else {
         costp = (59 * $('#anzahl_der_personen').val()) - 59;
    }

for some reason it never comes to the elseif statement! I tried other ways is .lenght, .is(empty).. to see if the text field is empty but it always goes to the else statment! if the value is not 1 it goes to the else statment. What is the proper way to check if the value is empty or null, because if a user doesnt put anything in to the field I want the variable costp to be 0;

TooCooL
  • 20,356
  • 6
  • 30
  • 49
  • possible duplicate of [Check if inputs are empty using jQuery](http://stackoverflow.com/questions/1854556/check-if-inputs-are-empty-using-jquery) – Manse Jan 04 '13 at 18:14

2 Answers2

2

JavaScript does not have an elseif statement. Use else if instead.

Also, you're using $.trim wrong.


Finally, there's no need for all that redundant code. Try this:

var costp;
var anzahl_der_personen = +$('#anzahl_der_personen').val();

if ( ! anzahl_der_personen || anzahl_der_personen === 1 ) {
    costp = 0;
} else {
    costp = (59 * anzahl_der_personen) - 59;
}
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
2

There should be a space between else if

Also check for empty string rather than null value

There is a syntax error in the way you are using the $.trim method . It expects a string.

   else if( $.trim(anzahl_der_personen) === '' ){
Sushanth --
  • 55,259
  • 9
  • 66
  • 105