How can I replace/substitute an attribute name, not its content

user3313627

On my site, each post starts with an image:

<img src="sample.jpg" alt="sample alt" />

and what I'm looking for is to change all image entries to:

<img data-src="sample.jpg" alt="sample alt" />

So basically I want to either:

  • Append/prefix src with data-

or

  • Replace/substitute src with data-src

I've looked at .replace() .replaceWith() .before() .insertBefore() and none of them did what I wanted.

A bit of info on why I want to do this. I want to implement unveil.js (lazyload) and one of the requirements is to include the actual image source in a "data-src" attribute. I don't want to mix html with markdown, hence this question.

Arun P Johny

I think the best that can be done is

$('img[src]').attr('data-src', function(){
    return $(this).attr('src')
}).removeAttr('src');

Demo: Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I replace/substitute an attribute name, not its content

From Dev

How can I read a cookie with a "-" in its name?

From Dev

How to handle attribute with @ in its name

From Dev

How can I split a string depending on its content?

From Dev

How can I delete the "sheet" node keeping its content intact?

From Dev

How can I make the ul contain li in its content box?

From Dev

How can I get the extension(s) of a file based on its content?

From Dev

How can I delete the "sheet" node keeping its content intact?

From Dev

How can I select an element using its text content?

From Dev

How can I use jQuery to replace the content of an attribute inside of a textarea?

From Dev

How to set attribute to element from its content?

From Dev

How can I tail a log file with the date appended to its name?

From Dev

How can I use a parameterized query to search on a column by its name?

From Java

How can I include a header file that contains `>` in its name?

From Dev

How can I retrieve a nested type with its fully qualified name?

From Dev

How can I cd to a directory without writing its name?

From Dev

How can I create zip file with the date in its name?

From Dev

Angular JS - How can I get the ngModelController by its model name?

From Dev

How can I assign a value to a variable by passing a string of its name?

From Dev

How can I get the name of the team instead of its ID

From Dev

How can I convert a channel/server ID to its name?

From Dev

How can I delete an ActiveX Button with VBA by its name?

From Dev

How can I cd to a directory without writing its name?

From Dev

How can I tail a log file with the date appended to its name?

From Dev

How can I create zip file with the date in its name?

From Dev

How can I retrieve a nested type with its fully qualified name?

From Dev

How can I search a file by its name and partial path?

From Dev

How can I drop a PostgreSQL table with double quotes in its name?

From Dev

How can i get the name of the interface, its type and ip?

Related Related

  1. 1

    How can I replace/substitute an attribute name, not its content

  2. 2

    How can I read a cookie with a "-" in its name?

  3. 3

    How to handle attribute with @ in its name

  4. 4

    How can I split a string depending on its content?

  5. 5

    How can I delete the "sheet" node keeping its content intact?

  6. 6

    How can I make the ul contain li in its content box?

  7. 7

    How can I get the extension(s) of a file based on its content?

  8. 8

    How can I delete the "sheet" node keeping its content intact?

  9. 9

    How can I select an element using its text content?

  10. 10

    How can I use jQuery to replace the content of an attribute inside of a textarea?

  11. 11

    How to set attribute to element from its content?

  12. 12

    How can I tail a log file with the date appended to its name?

  13. 13

    How can I use a parameterized query to search on a column by its name?

  14. 14

    How can I include a header file that contains `>` in its name?

  15. 15

    How can I retrieve a nested type with its fully qualified name?

  16. 16

    How can I cd to a directory without writing its name?

  17. 17

    How can I create zip file with the date in its name?

  18. 18

    Angular JS - How can I get the ngModelController by its model name?

  19. 19

    How can I assign a value to a variable by passing a string of its name?

  20. 20

    How can I get the name of the team instead of its ID

  21. 21

    How can I convert a channel/server ID to its name?

  22. 22

    How can I delete an ActiveX Button with VBA by its name?

  23. 23

    How can I cd to a directory without writing its name?

  24. 24

    How can I tail a log file with the date appended to its name?

  25. 25

    How can I create zip file with the date in its name?

  26. 26

    How can I retrieve a nested type with its fully qualified name?

  27. 27

    How can I search a file by its name and partial path?

  28. 28

    How can I drop a PostgreSQL table with double quotes in its name?

  29. 29

    How can i get the name of the interface, its type and ip?

HotTag

Archive