shin shin - 3 years ago 114
jQuery Question

How to add <br /> after Artikler with jquery

The following source is created dynamically.

I'd like to add

<br />
after Artikler, to become
Artikler<br />.


<div id="shopping">
<p><!--TEXT:--> <A HREF="cart-8.asp" CLASS="longsum">7&nbsp;Artikler, verdi:&nbsp;10 199,00</A></p>

I think I need to use

.after('<br />');

but I am not sure how to get Artikler.

7  will be changed dynamically.

Answer Source

The jQuery DOM manipulation functions (such as after()) work specifically on DOM elements, and in this case your entire block of text is a single element.

A better approach is to get the text out of the element:

var text = $("a.longsum").html();

Then do your string replace:

text = text.replace("Artikler", "Artikler<br />");

and put the result back into the element:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download