1

HTML:

<div class="form-group">
    <div id="errorsum"></div>
</div>

JavaScript:

 var errornbr = 0;
 var mess;
 $("#addclient  input.form-control").each(function (index) {
     if ($(this).val() == "") {
         if ($(this).attr("data-val") != undefined) {
             if ($(this).attr("data-val-required") != undefined) {
                 errornbr = errornbr + 1;
                 mess = mess + "<br/>" + $(this).attr("data-val-required");
             }
         }
     }
 });
 $("#errorsum").empty();
 if (errornbr > 0) {
     $("#errorsum").append("<label class='col-xs-3 control-label text-danger' >" + mess + "</label>");
 }

First line of the label text is always undefined. the mess didn't contains such value.

So I need to know:

  1. Does the empty() method is the reason of this?
  2. How can I fix this issue?
3
  • 6
    change var mess; to var mess = ''; Commented May 20, 2016 at 6:52
  • 1
    @guradio thank you very much, plz post your comment as an answer Commented May 20, 2016 at 6:53
  • 1
    Why not append to current value? mess += "<br/>" + $(t....... Commented May 20, 2016 at 6:56

2 Answers 2

3

change var mess; to var mess = '';

Make the assume text value making it not return undefined

Sign up to request clarification or add additional context in comments.

Comments

0

Please use ( typeof VALUE != 'undefined' ) instead of ( VALUE != undefined )

Please write condition like this

if( typeof $(this).attr("data-val") != 'undefined' ){

 if( typeof $(this).attr("data-val-required") != 'undefined' ){

 }

}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.