Just copying the comments, data
method does not set or change the attribute value, it amends the internal data structure of the element, which is not displayed as changed data-*
attribute.
So, if you need a markup change then use .attr('data-id', 'whatever-value')
instead. Otherwise use .data()
method, as you do, but check the correctness with picking up the value back, i.e. console.log(el.data('id'))
.
P.S.: As a side note, I'd recommend you to use closest()
method instead of parents()
. It will rapidly pick up the first matched parent element, instead of selecting all matched parent elements.