-2

I need to display a link in sweetalert html only if the var is not empty. Here is the code:

$('.patient-details').click(function(e) {
e.preventDefault();
var $this = $(this)
var name = $this.data('name');
var gender = $this.data('gender');
var age = $this.data('age');
var country = $this.data('country');
var address = $this.data('address');
var report = $this.data('report');
swal({
title: name,
html:
    "Gender: " + gender +"<br>" +
   "Age: " + age +"<br>" +
   "Country: " + country +"<br>" +
   "Address: " + address +"<br>" +
   (report!=undefined?'<a href="' + report + '" target="_blank">View Report</a>':''),
});
});

I need the report link to be displayed only if var report is not empty. Here is the code pen: https://codepen.io/pamela123/pen/GOJZgo

I tried

if(report){
report = $this.data('report');
}

report is "undefined". report!=undefined is not working.

But how not to display the report link inside the html if report is empty ??

I know it is a simple javascript question, but being a newbie i could not get further.

Pamela
  • 684
  • 1
  • 7
  • 21

3 Answers3

1

You Can Update your Code as below

var htmlTemplate="Gender: " + gender +"<br>" +"Age: " + age +"<br>" + "Country: " + country +"<br>" + "Address: " + address +"<br>" ;
if(report){
   htmlTemplate+= '<a href="' + report + '" target="_blank">View Report</a>';
}
swal({
title: name,
html: htmlTemplate,
});
Nitishkumar Singh
  • 1,781
  • 1
  • 15
  • 33
  • Why is report !== undefined not working??? Your solution works. But my question was why undefined is not working ?? – Pamela Oct 31 '17 at 12:33
  • Since you are passing **data-report=""**, that is why it's not working for undefined – Nitishkumar Singh Oct 31 '17 at 12:45
  • if( report !== undefined && report != "" ) will work. As i am getting the value from a DB. In some cases it may be empty. Please See the accepted answer. – Pamela Oct 31 '17 at 12:56
  • If You remove report !="", that code will also not work. I would recommend you to read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators, you will get more clarity on conditions – Nitishkumar Singh Oct 31 '17 at 13:03
1

Put the data in a separate variable.

Then check if report is not undefined. If not, add it to the variable.

$('.patient-details').click(function(e) {
  e.preventDefault();
  var $this = $(this)
  var name = $this.data('name');
  var gender = $this.data('gender');
  var age = $this.data('age');
  var country = $this.data('country');
  var address = $this.data('address');
  var report = $this.data('report');

  var htmlData = "Gender: " + gender + "<br>" +
      "Age: " + age + "<br>" +
      "Country: " + country + "<br>" +
      "Address: " + address + "<br>";
  if( report !== undefined && report != "" ) {
    htmlData +=  '<a href="' + report + '" target="_blank">View Report</a>'
  }
  swal({
    title: name,
    html: htmlData
  });
});
GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
0
$('.patient-details').click(function(e) {
e.preventDefault();
var $this = $(this)
var name = $this.data('name');
var gender = $this.data('gender');
var age = $this.data('age');
var country = $this.data('country');
var address = $this.data('address');
var report = $this.data('report');
swal({
title: name,
html:
    "Gender: " + gender +"<br>" +
   "Age: " + age +"<br>" +
   "Country: " + country +"<br>" +
   "Address: " + address +"<br>" +(typeof report !== "undefined" && report != ""?'<a href="' + report + '" target="_blank">View Report</a>':'')
    ,
});
});
Sandeep
  • 413
  • 4
  • 13