Dynamic Data() Jquery Add

Asked

Viewed 43 times

0

I would like to know the following. I am using the jquery’s data function. As HTML code is dynamically created, I cannot assign the date.

//Aqui  tem um each
$('#div').append('<span class="vor" data-tipo_m="'+v.cob_tipo_mapa+'">'+v.cob+'</span>');

I tried to take the last value of the class .vor and adds the date function. But it was not possible. So I added manually, as in the example above:

$('.vor:last').data("tipo_m", v.cob);

My question is, it will work in any browser. Because the jquery date() when I inspect the element does not appear. Already the way I did create the attribute and then get the value with date() of jquery.

1 answer

2


The command $('.vor:last').data("tipo_m", v.cob); that you used does not add values but recovers them.

To add use something like: jquery.data($('.vor:last'), "tipo_m", v.cob);. Note that for the method jquery.data(...) add elements it needs three parameters in your signature.

See jquery API reference here.

There is no problem in doing it the way you did. Using the date is just an alternative. Note that current jquery does not support older browsers such as IE 6 (not sure about IE 7).

  • 1

    There is no problem in doing it the way you did. Using the date is just an alternative. Note that current jquery does not support older browsers such as IE 6 (not sure about IE 7).

  • If the answer I have solved your problem please mark it as correct by marking the green tick. Thank you ;)

  • 1

    jQuery 2.x supports only IE 9+, so it does not support IE 8. jQuery 1.x supports IE 6+.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.