0

I need to change the following text "Error(s) encountered" to "Veuillez vérifier l’information suivante:" I have not been able to do this without losing formatting in the other text.

The items in the unordered list below "Error(s) encountered" is not constant, it changes based on what the users leaves out

<div style="" class="BBFormValidatorSummary" id="PC17178_formWizard_formWizard_ctl08">
Error(s) encountered:
<ul>
<li>Email: Required</li>
<li>Email: Required</li>
<li>Cardholder's Name: Required</li>
<li>Credit Card Number: Required</li>
<li>Credit Card Type:
</ul>
</div>

This is what I tried but it removes the formatting:

$('#PC17178_formWizard_formWizard_ctl08').each(function() {
var text = $(this).text();
$(this).text(text.replace('Error(s) encountered:', 'Veuillez vérifier l’information suivante:')); 
});

Here is the fiddle: http://jsfiddle.net/jelane20/y3Ej4/

Jenny
  • 781
  • 1
  • 9
  • 24
  • 1
    Why are you using `.each()` for an ID selector? There can only be one element with that ID, so there's nothing to loop over. – Barmar Jun 10 '14 at 20:38

4 Answers4

2

The problem is you are using .text(), which removes HTML elements from the string. Instead, if you use .html(), it keeps the HTML elements intact:

$('#PC17178_formWizard_formWizard_ctl08').each(function() {
    var text = $(this).html();
    $(this).html(text.replace('Error(s) encountered:', 'Veuillez vérifier l’information suivante:')); 
});

Improved Fiddle: Fiddle

Anonymous
  • 11,748
  • 6
  • 35
  • 57
1

Use .html() instead of .text().

Demo: http://jsfiddle.net/y3Ej4/2/

XCS
  • 27,244
  • 26
  • 101
  • 151
1

Simply use .html instead.

Alternatively, you can attempt to find the first text node and modify that directly. This would be slightly safer than modifying the entire HTML block, as then we can be sure we're not replacing something unintended.

Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236
1

Put a span around the text, so you can select it by itself.

<div style="" class="BBFormValidatorSummary" id="PC17178_formWizard_formWizard_ctl08">
<span class="error_prefix">Error(s) encountered:<span>
<ul>
<li>Email: Required</li>
<li>Email: Required</li>
<li>Cardholder's Name: Required</li>
<li>Credit Card Number: Required</li>
<li>Credit Card Type:
</ul>
</div>

Then you can do:

$("#PC17178_formWizard_formWizard_ctl08 .error_prefix").text('Veuillez vérifier l’information suivante:');
Barmar
  • 741,623
  • 53
  • 500
  • 612