jquery, replace multiple elements recursively -
i got stuck following issue. i'm explaining content footnotes. footnotes e.g. [10] have format <sup>[10]</sup>
. screenshot of content footnotes
i'm trying replace <sup>[10]</sup>
<a href="#m10" data-toggle="modal"><sup>[10]</sup></a>
using following jquery:
<script type='text/javascript'> $(window).on('load', function() { $(function(){ var s = $('sup').html().slice(1, -1); $("sup").replacewith('<a href="#m'+s+'" data-toggle="modal"><sup>['+s+']</sup></a>'); }); }); </script>
unfortunately code replaces footnotes same/first found footnote - in case, footnotes replaced "10" (see screenshot). want recursively replace each footnote.
i don't know how solve problem got stuck several days. perhaps of guys or girls can me. many in advance.
iterate on each of sup
elements , replace corresponding html rather replacing all sup
elements. inside of .each()
method, this
refers current sup
element.
based on jquery provided, want:
$('sup').each(function() { var s = this.textcontent.slice(1, -1); $(this).replacewith('<a href="#m' + s + '" data-toggle="modal"><sup>[' + s + ']</sup></a>'); });
rather using .slice()
, extract digit(s) using .match()
method:
$('sup').each(function() { var s = this.textcontent.match(/\d+/)[0]; $(this).replacewith('<a href="#m' + s + '" data-toggle="modal"><sup>[' + s + ']</sup></a>'); });
Comments
Post a Comment