Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to Set Element Attribute Using jQuery data()

I'm having trouble updating elements from JavaScript. My project is fairly complex but here's a tiny piece of it:

myWebService(/*...*/)
.done(function (result) {
    var newRows = $(rows).clone();
    $('#drop-placeholder').after(newRows);
    $(newRows).data('test', 'ZZZZZZZZZZZZZZZZZZZZZZZ');
}

Using Chrome, I can watch these statements execute. However, with newRows in the watch window, I can see that the attribute never gets added/updated. And, sure enough, after the call is done, I don't see the specified attribute on my page.

Can anyone see what I might be missing. rows (and therefore newRows) represents any number of table row elements.

like image 217
Jonathan Wood Avatar asked Sep 26 '26 01:09

Jonathan Wood


1 Answers

.data() sets background data that is handled purely by jQuery - it does not set HTML attributes.

If you are trying to set an attribute such as data-test in <div data-test="stuff"></div> you need to use .attr("data-test", variable).

like image 188
Ken Herbert Avatar answered Sep 28 '26 14:09

Ken Herbert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!