-1

if (!mileage) {
  valid = 0;
  plateErrors = "Please tell us the Vehicle Mileage";
}
if (!price) {
  valid = 0;
  plateErrors = "We can't generate the report with out Price ";
}

if (valid == 1) {

  window.open('https://www.carreport.ae/Home/VehicleInformationByPlate?siteID=1004' +
    '&plateSource=' + plate_source + '&plateCode=' + plate_code + '&plateNumber=' + plate_number + '&mileage=' + mileage + '&price=' + price, '_blank');

} else {
  var i;

  alert(plateErrors);
  $('#plateNumberError').removeClass('hidden');
  //plateErrors.toString();

  $.each(plateErrors, function(key, element) {
    $("#plateNumberError").html("<p class='alert alert-danger' role='alert'>" + key + "</p>");
  });

}

Above is my sample code:

what i need is I am having an array with error texts .I need to display each text in a P tag and insert all these P tags with error msg to a div.

When I use a for loop i am getting only the characters , not the whole string as one

Emanuele Parisio
  • 1,192
  • 8
  • 27
Arun
  • 3
  • 4

4 Answers4

1

make it

var plateErrors = [];
if (!mileage)
{
   valid = 0; 
   plateErrors.push( "Please tell us the Vehicle Mileage" ); 
} 
if (!price) 
{ 
   valid = 0; plateErrors.push( "We can't generate the report with out Price " ); 
} 
if (valid == 1) 
{ 
  window.open('https://www.carreport.ae/Home/VehicleInformationByPlate?siteID=1004'
+ '&plateSource=' + plate_source + '&plateCode=' + plate_code + '&plateNumber=' + plate_number + '&mileage=' + mileage + '&price=' + price, '_blank'); 
} 
else
{ 
  var i; 
  alert(plateErrors); 
  $('#plateNumberError').removeClass('hidden'); //plateErrors.toString();
  $.each(plateErrors ,function(key,element){ 
    $("#plateNumberError").append( "<p class='alert alert-danger' role='alert'>" + element + "</p>"); }); 
}
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

You need to reaplace .html()by .append() :

$.each(plateErrors ,function(key,element){
     $("#plateNumberError").append("<p class='alert alert-danger' role='alert'>" + key + "</p>");
});

It should works

Yoplaboom
  • 554
  • 3
  • 13
0

you need to create an array first:

var plateErrors = [];
if (!mileage)
{
   valid = 0; 
   plateErrors.push("Please tell us the Vehicle Mileage");
} 
if (!price) 
{ 
   valid = 0; 
   plateErrors.push("We can't generate the report with out Price); 
} 
Johannes Jander
  • 4,974
  • 2
  • 31
  • 46
0

Use .html() instead of .append() to append HTML elements. This is the fastest way to append elements to DOM.

plateErrorsHtml = [];

$.each(plateErrors ,function(key,element){ 
    plateErrorsHtml.push("<p class='alert alert-danger' role='alert'>" + element + "</p>");
}); 

$("#plateNumberError").html(plateErrorsHtml.join(''));

Here is the detailed discussion on this topic jquery .html() vs .append()

Community
  • 1
  • 1
Narendra CM
  • 1,416
  • 3
  • 13
  • 23