wrap $ in span using jQuery

Trisha

I'm trying to wrap any dollar signs $ in a span so that I can style them accordingly, without having to go back through and editing each $ I find on the page. The code I've tried to use was for ampersands, and works perfect for ampersands, but I can't get it to work correctly with a dollar sign!

What am I doing wrong here? I've tried using just the $ symbol, and &#36;, but neither are giving me the results needed. Instead it doesn't touch the $ that is wrapped in the <p>, but instead adds it right before the closing </p>.

JSFiddle: https://jsfiddle.net/tk8og3Ln/

HTML:

<p>Cabana Tanning Lotion: $32.00</p>

jQuery:

(function($) {
  $.fn.money = function() {
    return this.each(function() {
      var element = $(this);
      var html = element.html();
      element.html(html.replace(/$/gi, '<span class="money">$</span>'));
    });
  };
})(jQuery);

$('p').money();

CSS:

.money {
  font: italic 1.3em Baskerville, Georgia, serif;
  color: #666;
  padding-right: 5px;
}
Trisha

Going off of the answer from @guest271314, I also needed to make sure to wrap the code in a document.ready function to allow WordPress to initialize the jQuery. Here is the finished code.

jQuery(document).ready(function( $ ) {
  $.fn.money = function() {
    return this.each(function() {
      var element = $(this);
      var html = element.html();
      element.html(html.replace(/\$/gi, '<span class="money">$</span>'));
    });
  };

$('p').money();

});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Replace a single letter wrap with span element using jquery

From Dev

Wrap a span around link text with jQuery

From Dev

JQuery wrap span around text on click

From Dev

Using the jQuery Validate plugin, how can I wrap the error message string in a span

From Dev

jQuery wrap text in a span before and after br within a paragraph

From Dev

With jquery, match single character followed by colon and wrap in span tag

From Dev

jQuery UI Slider - Min & Max values wrap in <span> tag

From Dev

JQuery if line of text starts with character, wrap whole line in span

From Dev

Using jQuery to wrap elements in a hyperlink

From Dev

Wrap groups of elements using jQuery

From Dev

Using jQuery to wrap elements in a hyperlink

From Dev

Wrap groups of elements using jQuery

From Dev

How to wrap headers within a span from specific division using javascript

From Dev

Wrap each span in a container

From Dev

A tag will not wrap the whole span

From Dev

Wrap with span if text is selected

From Dev

How do I append span to span present in html using jquery?

From Dev

How to get the text after a span using jquery (multiple span)

From Dev

Add <br> in between </span> and <span> using Jquery/Javascript

From Java

Animate the digits into the separate span using CSS and jQuery

From Dev

How to get the inner span id using JQuery

From Dev

Using jQuery to add a ✔ tick mark in span

From Dev

I cannot get the id of the span using jquery

From Dev

replace text string with span using jQuery

From Dev

How to change text in span using Jquery

From Dev

Select the correct span and div in jquery using classes

From Dev

Using SignalR and jquery to change css on a span

From Dev

Jquery Using PrevAll to find previous span

From Dev

Switch span text using JQuery in a table

Related Related

  1. 1

    Replace a single letter wrap with span element using jquery

  2. 2

    Wrap a span around link text with jQuery

  3. 3

    JQuery wrap span around text on click

  4. 4

    Using the jQuery Validate plugin, how can I wrap the error message string in a span

  5. 5

    jQuery wrap text in a span before and after br within a paragraph

  6. 6

    With jquery, match single character followed by colon and wrap in span tag

  7. 7

    jQuery UI Slider - Min & Max values wrap in <span> tag

  8. 8

    JQuery if line of text starts with character, wrap whole line in span

  9. 9

    Using jQuery to wrap elements in a hyperlink

  10. 10

    Wrap groups of elements using jQuery

  11. 11

    Using jQuery to wrap elements in a hyperlink

  12. 12

    Wrap groups of elements using jQuery

  13. 13

    How to wrap headers within a span from specific division using javascript

  14. 14

    Wrap each span in a container

  15. 15

    A tag will not wrap the whole span

  16. 16

    Wrap with span if text is selected

  17. 17

    How do I append span to span present in html using jquery?

  18. 18

    How to get the text after a span using jquery (multiple span)

  19. 19

    Add <br> in between </span> and <span> using Jquery/Javascript

  20. 20

    Animate the digits into the separate span using CSS and jQuery

  21. 21

    How to get the inner span id using JQuery

  22. 22

    Using jQuery to add a ✔ tick mark in span

  23. 23

    I cannot get the id of the span using jquery

  24. 24

    replace text string with span using jQuery

  25. 25

    How to change text in span using Jquery

  26. 26

    Select the correct span and div in jquery using classes

  27. 27

    Using SignalR and jquery to change css on a span

  28. 28

    Jquery Using PrevAll to find previous span

  29. 29

    Switch span text using JQuery in a table

HotTag

Archive