Ecommerce Shopify WordPress Discussion

Replace <i> elements with <span> elements

I want to replace all "i" elements with "span" elements. I try many things but it didn't work. I didn't find either error messages and debug JavaScript code in my browser. I want to replace for example: <i class="icon-user"></i> to <span class="icon-user"></span> I try those but nothing works: 1) (function($) { $(document).ready(function() { // Replace <i> elements with <span> elements $('i.icon-user').replaceWith('<span class="icon-user"></span>'); }); })(jQuery); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <p>This is an icon <i class="icon-user"></i> and this as well <i class="icon-user"></i> </p> Run code snippetExpand snippet 2) jQuery(document).ready(function($) { // Check if jQuery is loaded if (typeof jQuery === 'undefined') { console.error('jQuery is not loaded.'); return; } // Check if the element exists if ($('i.icon-user').length === 0) { console.error('The element with class "icon-user" does not exist.'); return; } // Replace <i> with <span> $('i.icon-user').each(function() { $(this).replaceWith('<span class="icon-user"></span>'); }); }); <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script> <p>This is an icon <i class="icon-user"></i> and this as well <i class="icon-user"></i> </p> Run code snippetExpand snippet 3) document.addEventListener('DOMContentLoaded', function() { var iconUsers = document.querySelectorAll('i.icon-user'); iconUsers.forEach(function(iconUser) { var spanElement = document.createElement('span'); spanElement.className = 'icon-user'; iconUser.parentNode.replaceChild(spanElement, iconUser); }); }); <p>This is an icon <i class="icon-user"></i> and this as well <i class="icon-user"></i> </p> Run code snippetExpand snippet
Here's an approach utilizing regex to replace the tag name but keep its structure document.addEventListener('DOMContentLoaded', function() { document.querySelectorAll('i.icon-user').forEach(function(iconUser) { iconUser.outerHTML = iconUser.outerHTML.replace(/(?:(?<=^<)|(?=\w+>$))\b\w+\b/g, 'span') }); console.log(document.body.innerHTML); }); <i class="icon-user"></i> <div> <i id="foo" class="icon-user"> <span>test</span> </i> <i class="bar"></i> </div> Run code snippetExpand snippet

February 13, 2024

Not using JQuery, you can use Element.replaceWith. Here's an example. setTimeout(replaceIWithSpan, 1500); function replaceIWithSpan() { document.querySelectorAll(`i.icon-user`) .forEach( i => { const span = document.createElement(`span`); // copy attributes from <i> to <span> for (const attr of i.attributes) { span.setAttribute(attr.name, attr.value); } span.innerHTML = i.innerHTML; console.log(`${i.outerHTML} replaced with\n=> ${span.outerHTML}`); // replace i.replaceWith(span); }); } span.icon-user { color: green; font-weight: bold; } i.icon-user { color: red; } <div><i class="icon-user" id="el1">#1</i></div> <div><i class="icon-user" id="el2">#2</i></div> <div><i class="icon-user" id="el3">#3</i></div> Run code snippetExpand snippet

February 13, 2024

The Script: $(document).ready(()=>{ let a=$("<span></span>").addClass('icon-user').text('abc'); a.replaceAll("i.icon-user"); }); It works. Try and reply. Glad to help.

February 13, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.