problem getting the javascript data value

0

I have a problem getting data from a javascript data

In the html I have the following line

<span id="internet-compl" class="internet-item" data-precio="${empty elem.extraVl?0:elem.precio }" data-cantidad="1" data-vel="${elem.velocidad}" data-promo="${elem.tienePromoMensual}" data-descpromo="${elem.promo.nombrePromocion}">${elem.titulo}</span>

and in the java script I have the following

        var promo = $("#internet-compl").html($(this).data("promo"));
        console.log(promo);

The problem is that instead of getting only the string that I assign to the data I get the whole HTML element as shown below

Someone has an idea that I'm doing wrong

Note there is no error in the part of assigning elem.tienePromoMensual that is an object that I am extracting with JSTL

    
asked by Polo 24.10.2017 в 22:21
source

2 answers

1

If you only want to obtain the value of one of its attributes, you should directly access it

var promo = $("#internet-compl").data("promo");
console.log(promo);
    
answered by 24.10.2017 / 22:53
source
0

You can run directly as explained to you

var promo = $("#internet-compl").data("promo");
console.log(promo);

In case you manage another custom tag you could use

var promoAttr = $("#internet-compl").attr("data-promo");
console.log(promoAttr);

With the difference that accessing with .data () you will get the value according to the data type if it is number or string

While with .attr () you'll get everything as a string

you can check it with a typeof () depending on what you need

    
answered by 31.10.2017 в 03:53