I am dynamically changing the data-attribute of a couple of elements. An 'img' and an 'a' type. The 'img' element works with the following code:
$("img[data-image-index-no-"+self.options.typeOfImage+"="+nextIndexNo+"]").data("image-index-no-"+self.options.typeOfImage, newIndexNo);
I then wanted to change an 'a' link element and wrote:
$("a[data-slide-index="+nextIndexNo+"]").data("slide-index", newIndexNo);
The above didn't work but the I got it to work using:
$("a[data-slide-index="+nextIndexNo+"]").attr("data-slide-index", newIndexNo);
I would like to know why using the .data
works with the img element but not the 'a' element? And which method is the preferred way?
Any ideas on this?