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.
areyouready344Asked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.