iagdotme
iagdotme

Reputation: 993

How do I append the attribute value to each element on a page?

I want to append a link to each element on a page that has the attribute name "data-update-id"

On the page there are multiple li elements like this:

<li class="feed-update member-like-share  has-snippets" data-update="true" data-block="update" data-update-id="6029870346117619712" data-actor-type="member" data-li-utrk...

I want to grab the value of data-update-id for each one (eg 6029870346117619712) in the above example and append it to the li element. The added complication is that I want to append this number to a url and make it a link.

The link is https://www.linkedin.com/nhome/updates?topic=, and so in the above example I'd like it to become https://www.linkedin.com/nhome/updates?topic=6029870346117619712

I tested the following to start with and it doesn't work:

 "li[data-update-id]").append($(this).attr("data-id"));

I'm still fairly new to jQuery, so be kind. I assume it doesn't work because I would need to loop round each element? Can you help?

Upvotes: 0

Views: 28

Answers (2)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

You may use each , html and using data to grab the data-* attribute.

$("li[data-update-id]").each(function(){
   $(this).html($(this).data('update-id'));
});

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82241

You can use callback function provided for .text():

$('li').text(function(i,oldtxt){
  return oldtxt + $(this).data("update-id");
});

Working Demo

Upvotes: 1

Related Questions