0

I'm trying to replace text on a site using this code:

<script type="text/javascript">
    $(document).ready(function () {
        $(function() {
            $(document.body).find('*').each(function() {
                var tmp = $(this).children().remove();
                var text = $(this).text();
                text = text.replace(/Vorname/g, "Firstname");
                $(this).text(text);
                $(this).append(tmp);
            });
        });
    });

</script>

HTML (snippet) is like this (generated by SharePoint):

<td style="padding:4px;">
<div>
    <label for="ctl00_IWS_WH_CPH_Content_ContactUsControl1_firstName">
        <span><font color='red'>*</font> Vorname (erforderlich):</span>
    </label>
</div>

The above jQuery code is not being executed... Reference to jQuery has been placed in the master page.

Any clue why this is not working?

Regards, Thomas

1
  • 1
    So your starting with the body, removing all children of all elements, and then you'd like to replace the text where exactly? Commented Nov 5, 2012 at 10:08

4 Answers 4

1

I have found you are using two doc ready functions you have to use only one:

Either : $(document).ready(function () {

or :     $(function() {

and see if this works.

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

Right. "$(document).ready(function () { " and "$(function() {" is the same thing.
1

working demo

check the jsfiddle link for the working code

 $(document).ready(function () {
$(document.body).find('*').each(function() {
                var tmp = $(this).children().remove();
                var text = $(this).text();
                text = text.replace(/Vorname/g, "Firstname");
                $(this).text(text);
                $(this).append(tmp);
            });
   });

Comments

1
$('.element').text("sometext");

i think this is not right

var text = $(this).text();
                text = text.replace(/Vorname/g, "Firstname");

you should use:

var text = $(this).text();
    text.replace("Vorname", "Firstname");

then you should document better your question, i don't understand your real problem.

Comments

0

In your code, you are stripping off some elements style. If you want to keep layout you better have to use html not text, but it really depends of what you are looking for.

$(document).ready(function () {
   $('body').find('*').each(function() {                
       var html= $(this).html().replace(/Vorname/g, "Firstname");
        $(this).html(html);
    });
 });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.