Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 255
  • Last Modified:

how to append additional custom data attribute to an already created element?

how to append additional custom data attribute to an already created element?

For example, this li element is create.

$("#list").append('<li data-keyValue="' + keyValue + '">' + dataValue +'</li>');

After creating it, I would add an additional custom data attribute, say, data-firstKeyValue

$("#list li:first").append("<data-firstKeyValue>"); <--- this is not working.

Afterward, how would I go back to the previous created data-firstKeyValue?

$("#list li").attri("+ firstKeyValue +").prev(); <-- not working.
0
areyouready344
Asked:
areyouready344
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Use : $("#list li:first").attr("data-firstKeyValue", "the_value_you_want_to_set");
0
 
areyouready344Author Commented:
thanks again leakim, also, how do I go back to a previous data-firstKeyValue using jquery prev() method?
0
 
leakim971PluritechnicianCommented:
it's not a good use of yhe prev() method

you need to save it somewhere yourself as usual
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now