javascript - Replace every <img> tag with itsown custom data attribute -


how replace every occurrence of <img> tag in html message, unicode value stored custom attribute.

sample message:

<img data-uni-val="&#x1f603;" src="path/to/img1.png" class="emoji"/>hello, <br /> <img data-uni-val="&#1f604;" src="path/to/img2.png" class="emoji"/> 

i need replace every emoji <img> unicode value stored custom attribute.

$('<div />') .html(chattext).find('img.emoji') .replacewith('someval').end().html() 

using above code can find , replace every img's string, not able replace data-uni-val.

i tried:

$('<div />').html(chattext).find('img.emoji') .replacewith($(this) .data('data-uni-val')).end().html() 

is there simple way solve this?

the main issue code attribute data-uni-val should accessed using $(this).data('uni-val').

furthermore, use .replacewith(fn) perform conversion.

$('.emoji').replacewith(function() {    return $(this).data('uni-val');  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <img data-uni-val="&#x1f603;" src="path/to/img1.png" class="emoji"/>hello,  <br /> <img data-uni-val="&#x1f604;" src="path/to/img2.png" class="emoji"/>


Comments