How to show a checkbox if a variable has text, jquery?

1

Good morning. There is a variable with value that the server responds to. You can come like this:

DescripcionProductoGratis = ""

or like this:

DescripcionProductoGratis: "Tienes  Derecho a un Producto con Valor de: 460 - 9588 Lima de Pies"

So if the variable ProductDescriptionGratis has some content I have to show a div that contains a checkbox, for which I'm doing it this way:

if (response.data.DescripcionProductoGratis){
    $('.lista-productogratis').show();
    $('.DescripcionProductoGratis').html(response.data.DescripcionProductoGratis);
}

but it does not work for me.

Thank you in advance.

Here is the div that I used:

<div class="list-block no-hairlines no-hairlines-between lista-productogratis">
                    <div class="card data-table data-table-init">
                        <form id="productoGratis">
                            <table>
                                <thead>
                                    <tr>
                                        <td class="label-cell" style="padding: 0 !important;">
                                              <label class="label-checkbox item-content">
                                                <input type="checkbox" id="productogratis" class="productogratis" />
                                                <span class="item-media">
                                                    <i class="icon icon-form-checkbox"></i>
                                                </span>
                                                <span class="item-inner">
                                                    <span class="item-title DescripcionProductoGratis"></span>
                                                </span>
                                            </label>
                                        </td>
                                    </tr>
                                </thead>
                                <tbody></tbody>
                            </table>
                        </form>
                    </div>
                </div>
    
asked by JG_GJ 12.11.2018 в 16:08
source

2 answers

1

var response = "";
 if (response !=""){
    $('.lista-productogratis').show();
    $('.DescripcionProductoGratis').html("pruebaaa");
}
else
{
    $('.lista-productogratis').hide();
    $('.DescripcionProductoGratis').html("pruebaaa");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="lista-productogratis">
                    <div class="card data-table data-table-init">
                        <form id="productoGratis">
                            <table>
                                <thead>
                                    <tr>
                                        <td class="label-cell" style="padding: 0 !important;">
                                              <label class="label-checkbox item-content">
                                                <input type="checkbox" id="productogratis" class="productogratis" />
                                                <span class="item-media">
                                                    <i class="icon icon-form-checkbox"></i>
                                                </span>
                                                <span class="item-inner">
                                                    <span class="item-title DescripcionProductoGratis"></span>
                                                </span>
                                            </label>
                                        </td>
                                    </tr>
                                </thead>
                                <tbody></tbody>
                            </table>
                        </form>
                    </div>
                </div>

Try checking that it is different from empty due to the two options that show the answer that could come from the server, otherwise it hides:

  if (response.data.DescripcionProductoGratis != ""){
        $('.lista-productogratis').show();
        $('.DescripcionProductoGratis').html(response.data.DescripcionProductoGratis);
    }else {
$('.lista-productogratis').hide();
}
    
answered by 12.11.2018 / 16:15
source
0

Apparently the problem is in the if

if (response.data.DescripcionProductoGratis.length > 0){
    $('.lista-productogratis').show();
    $('.DescripcionProductoGratis').html(response.data.DescripcionProductoGratis);
}

This should solve the problem, check the length of the text, if the string has a length greater than 0 it will show

    
answered by 12.11.2018 в 16:19