1

I've one variable error_msgs. In this variable I've to add error messages. First error message will be simply get added to the variable. Later subsequent error messages will get concatenate to the previous error message. While concatenating the later subsequent error messages each error message should be prefixed with <br/> tag to show each error message on separate line.

I tried following code for it but I'm not able to do it.

var error_msgs = "Email is invalid !";

var error_msgs += "<br/>"+"Please enter First Name !";

var error_msgs += "<br/>"+"Please enter Last Name !";

alert(error_msgs);

I got following error in my firebug console for above code :

SyntaxError: missing ; before statement


var error_msgs + = "<br/>"+"Please enter First Name !";

Could someone please help me in resolving the error?

Thanks.

1
  • 2
    You only need var once. Commented Jan 11, 2015 at 7:48

2 Answers 2

2

You're initializating the variable too many times. It should be:

var error_msgs = "Email is invalid !";

error_msgs += "<br/>"+"Please enter First Name !";

error_msgs += "<br/>"+"Please enter Last Name !";

alert(error_msgs);
Sign up to request clarification or add additional context in comments.

Comments

1

Jack is correct in his comment

You only need var once.

The var keyword introduces a variable, with an optional value assigned.

The language (which is JavaScript, your question actually has nothing to do with jQuery) does not support the following line:

var error_msgs += "<br />" + "Please enter First Name !";

because it doesn't really make sense. You can define a variable and the initial value for it, but you can't really define a variable and append a value to it at the same time, since there's no value for it yet.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.