Know if a variable has no data

0

I have a modal to which I pass a variable by javascript, the fact is that this variable has no way of contouring when it is empty or has data. The step of variable data-url to which I assign convenio.url_also does it well, since from the modal if I do an alert and have data shows them.

As if it's worth something, variable in origin comes from a json of a php file that I call with the ajax function, as the field is rich text, to remove the remains of html I apply this:

$data->url_aso=strip_tags($data->url_aso);  //strip_tags elimina tags html de una cadena dada
    $data->url_aso=str_replace ("p, li { white-space: pre-wrap; }" , "" , $data->url_aso  ); //Esto lo  hago ya que despues del strip_tags queda este resto y lo sustituyo por ""

This is the function where the variable happened.

$.each(objJsonConBus, function(f, convenio){
        conv += '<li>'+
            '<div '"data-url="'+convenio.url_aso +'  "data-toggle="modal"  data-target="#myModal" >'+
                '<div class="card"><center><img class="img-responsive " src="'+convenio.base64_png+'" />'+
                '<h5 CLASS="descuento ">'+subDescuento+'</h5></center></div></div>'+
            '</li>';
        });

This is the modal that receives the variable.

 $('#myModal').on('show.bs.modal', function(e) {   
            var nameConv = $(e.relatedTarget).data('url'); 

              if(nameConv!="")
               {
                 alert(nameConv);
                }

    });

The way you tell me, always makes me the alert. Even if the alert shows me empty.

checking with typeof, if I put number it always tells me that it is false, if I put undefined, it also always tells me that it is false, it only tells me that it is true if I put string, that it always tells me that it is string, it gives me the impression that remains some kind of residue that does not appear in the alert but javaescript does not consider the variable as empty.

    
asked by Lorenzo Martín 09.03.2018 в 18:54
source

4 answers

5

There are several ways to know:

  //si es simple
    var variable1 = null;
    if(!variable1) {
       console.log('sin valor 1');
    }
    
    //si es un objeto
    var variable2 = {};
    if(Object.keys(variable2).length < 1) {
       console.log('sin valor 2');
    }
    
    //saber si un objeto tiene un campo
    function contains(propiedad, objeto) {
       for(key in objeto) {
         if(key == propiedad) {
           return true;  
         }
       }
       return false;
    }

You should also consider fake javascript values

    
answered by 09.03.2018 / 19:56
source
2

Because of the way you include the data-url in the div, you are sure that the url property will always be declared in the object returned by data, so all you have to do is check if it has content or not and in the case of strings is how you indicate with:

    if (variable != "") {
        // si tiene contenido
    } else {
        // si no tiene
    }
    
answered by 09.03.2018 в 19:06
1

the error is in this line

var nameConv = $(e.relatedTarget).data().url; 

you must use url in the data () method

var nameConv = $(e.relatedTarget).data('url'); 

there you get data-url

    
answered by 09.03.2018 в 19:00
1

You can know if the value was sent with the typeof

function ver(valor){
    console.log("viene el valor?" ,  (typeof(valor) !== 'undefined'))
}
ver(1)
ver()
    
answered by 09.03.2018 в 19:16