You need .each()
with .data()
, As .data()
is to get data-attribute
of the element
$(".a").each(function(){
var id = $(this).data('u');
$("#id"+id).text("23456") // Will set same text for both divs
});
As per your comment
If you prefer .attr()
then try this
var id = $(this).attr('data-u'); // .attr instead of .data()
Note: If you're using data-attr
anywhere in your mark-up, it is recommended to use .data()
instead of .attr()
to get that attribute.