How do I detect a certain word in one span and apply it to another span using jQuery?

badwolfff
badwolfff used Ask the Experts™
on
I have a dynamically generated code that resembles this:
<ul class="list-unstyled col-lg-12 no-padding">
	                         <li>
              <h2 class="priceUpdate"> da  €200.00</h2>
            </li>
			<span>
			<li>
				 <h2 class="priceUpdate"><i class="mfcustom-icon-twopay"></i>€193.00</h2>			  
			</li>
			</span>
            			
                      </ul>

Open in new window


I need to detect if in the h2.priceUpdate the string " da  " is present and if so insert it just after the <i class="mfcustom-icon-twopay"></i>

Could anyone help me work out how to achieve this via jQuery?

thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Ah! I forgot!

I also need to change that second h2 in an h5

thanks
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
This should do it - working sample here
<script type="text/javascript">
$(function() {
  // GET THE H2 ELEMENTs
  var h2 = $('.priceUpdate');
  
  // MATCH da ON THE FIRST
  var m = h2[0].innerHTML.match(/(\s+da\s+)/g);
  
  // IF WE HAVE A MATCH THEN
  if (m) {
    // CREATE A TEXT NODE WITH THE da AND ADD AFTER THE <i>
    $('i.mfcustom-icon-twopay').after(document.createTextNode(m[0]));
    
    // CREATE THE H5 ELEMENT WITH THE HTML FROM THE H2
    var h5 = $('<h5/>').html(h2[1].innerHTML).addClass('priceUpdate');
    
    // ADD THE H5
    $(h2[1]).parent().append(h5);
    
    // AND REMOVE THE H2
    $(h2[1]).remove();
  }
});
</script>

Open in new window

Author

Commented:
Thanks! I've learned a new trick or two here thanks to you!
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
You are most welcome.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial