Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can retrieve the source of an image using the attr() method in jQuery and then assign it to a data attribute using the data() method. Here's an example:

HTML:

<img src="example.jpg" alt="Example">

jQuery:

var imageUrl = $('img').attr('src');
$('img').data('image-src', imageUrl);

In this example, we first retrieve the source of the image using attr('src') and store it in the variable imageUrl. We then use the data() method to assign this value to a data attribute with the key image-src.

You can then retrieve this data attribute later using data('image-src'). For example:

var imageData = $('img').data('image-src');
console.log(imageData); // Outputs "example.jpg"