-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.

4
  • Just put a check if it is blank, then do not include the anchor. Commented Oct 31, 2017 at 12:07
  • It seems like your variable has the content "undefined" according to your question. This indicates that it is a string. Which will lead the if statement to be true Commented Oct 31, 2017 at 12:15
  • @ kevinSpaceyIsKeyserSöze Which is exactly my question. It is not working as can be seen in the codepen. codepen.io/pamela123/pen/GOJZgo Commented Oct 31, 2017 at 12:21
  • @Pamela can't see that the report value is undefined. Can you provide a codepen when it is undefined? Commented Oct 31, 2017 at 12:25

3 Answers 3

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
  });
});
Sign up to request clarification or add additional context in comments.

10 Comments

Sorry..Not working.
After changing if( report)..it will work. But not if report !== undefined. which was my question. Wonder why report !== undefined is not working ??
I have added && report. With that addition it works when the data-report attribute is empty and if the attribute isn't set at all (codepen.io/anon/pen/VrLPVQ)
Then try the code again, I've changed it in my answer
|
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,
});

4 Comments

Why is report !== undefined not working??? Your solution works. But my question was why undefined is not working ??
Since you are passing data-report="", that is why it's not working for undefined
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.
If You remove report !="", that code will also not work. I would recommend you to read developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…, you will get more clarity on conditions
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>':'')
    ,
});
});

4 Comments

report!=null? is not working.
use typeof report != "undefined" instead of that
Sorry..That is not working either.
Check the above code that will work (typeof report !== "undefined"?'<a href="' + report + '" target="_blank">View Report</a>':'')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.