Using Javascript to modify text

Hi. I have an element generated by a plugin which I want to alter by inserting an HTML prefix. An example of the element would be this:

<span class="cost">£25 – £32</span>

I want the output to change from

£25 – £32

to become:

Cost: £25 - £32

I tried adding this in the page’s Javascript section:

jQuery(document).ready(function(){
  jQuery('span.cost').before('Cost:&nbsp;');
});

… but it didn’t work. Can you tell me where I’m going wrong? I appreciate that this falls outside your regular support but hoping you don’t mind sharing your expertise with me!

Many thanks
Mike.

Hi Mike,

Please try adding just:

jQuery('span.cost').before('Cost:&nbsp;');

Hope this helps.

Hi Jade

Thanks for replying. That’s almost what I needed. The HTML becomes

Cost:&nbsp;<span class="cost">£25 – £32</span>

but what I need is

<span class="cost">Cost:&nbsp;£25 – £32</span>

Is this possible?
Mike.

Hi again Jade. I found the solution, it’s this:

jQuery('span.cost').prepend('<strong>Cost:</strong>&nbsp;');

Thanks for pointing me in the right direction.
Mike.

You’re welcome, Mike and glad to hear you got it sorted. :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.