Execute javascript function if condition is true

0

Good evening.

I have a button that when clicked, calls the function below:

$('#sa-success').click(function () {
   swal({
      title: 'Good job!',
      text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lorem erat, tincidunt vitae ipsum et, pellentesque maximus enim. Mauris eleifend ex semper, lobortis purus sed, pharetra felis',
      type: 'success',
      buttonsStyling: false,
      confirmButtonClass: 'btn btn-primary'
   });
});

But I wanted this function to be executed if a certain condition occurred in my IF. Example:

if(teste == true){
   $('#sa-success').click(function () {
      swal({
         title: 'Good job!',
         text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lorem erat, tincidunt vitae ipsum et, pellentesque maximus enim. Mauris eleifend ex semper, lobortis purus sed, pharetra felis',
         type: 'success',
         buttonsStyling: false,
         confirmButtonClass: 'btn btn-primary'
      });
   });
}

Is it possible?

    
asked by anonymous 30.11.2017 / 00:30

1 answer

1

Following the suggestion of @AndersonCarlosWoss it looks like:

  $('#sa-success').click(function () {
      if (teste !== true) {
        return;
      }
      swal({
         title: 'Good job!',
         text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed lorem erat, tincidunt vitae ipsum et, pellentesque maximus enim. Mauris eleifend ex semper, lobortis purus sed, pharetra felis',
         type: 'success',
         buttonsStyling: false,
         confirmButtonClass: 'btn btn-primary'
      });
   });
    
30.11.2017 / 01:24