Friday, September 16, 2011

Jquery Tip: How to wrap tags dynamically


Quick tip:

In order to wrap a <span> around an <a> tag for example:
<ul>
<li>item 1</li>
<li>item 2</li>
</ul>

to:

<ul>
<li><span>item 1</span></li>
<li><span>item 2</span></li>
</ul>
Apply this jquery call:
<script type="text/javascript">
$(document).ready(function() {
    $("li a").each(function() {
        $(this).wrap('<span/>');
    });
});
</script>

No comments: