1

I'm trying to replace text only, but without touching any other tags.

<p>
    <a href="login.php">
        <i class="fa fa-sign-in"></i> 
        Login
    </a>
</p>
$('p').each(function() {
    $(this).text($(this).text.replace('Login', 'Anmeldung')); 
});

Bad result:

<p>
    Anmeldung       
</p>

Result as I would like it to be:

<p>
    <a href="login.php">
        <i class="fa fa-sign-in"></i> 
        Anmeldung
    </a>
</p>

How can I do this? This is only a sample, deeper structure of p tags can be completely different.

Mohammad
  • 21,175
  • 15
  • 55
  • 84
user3532637
  • 333
  • 3
  • 16
  • If the only element you can select is the `p` you will need to recurse through the child elements and nodes and replace the `text()` within that element directly. Replacing the `text()` of the parent will have the effect of removing the HTML – Rory McCrossan Sep 21 '16 at 10:17
  • 2
    Possible duplicate of [JQuery change inner text but preserve html](http://stackoverflow.com/questions/5232862/jquery-change-inner-text-but-preserve-html) – BadHorsie Sep 21 '16 at 10:18
  • You want to change part of text or all of it? – Mohammad Sep 21 '16 at 10:37
  • Why only with jQuery? It's almost like you don't know that jQuery isn't a language... – evolutionxbox Sep 21 '16 at 10:37
  • @evolutionxbox jQuery is great for operations on the DOM. Why should he use a more complex solution with vanilla js if he's using jQuery in his application anyway? – Kilian Stinson Sep 21 '16 at 10:57
  • @KilianStinson - see http://stackoverflow.com/a/39613834/989920 – evolutionxbox Sep 21 '16 at 11:00
  • @evolutionxbox The answer you linked is using jQuery as solution and is wrong anyway. What's your point? – Kilian Stinson Sep 21 '16 at 11:12

6 Answers6

2

Wrap text you want to replace in a span or something.

<p>
    <a href="login.php">
        <i class="fa fa-sign-in"></i> 
        <span class="replace-login">Login</span>
    </a>
</p>

and js

    $('.replace-login').each(function() {
       $(this).text($(this).text.replace('Login', 'Anmeldung')); 
   });
Andrei Todorut
  • 4,260
  • 2
  • 17
  • 28
2

Use .html() instead of .text()
This preserves your html tags

$('p').each(function() {
    $(this).html($(this).html().replace('Login', 'Anmeldung')); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
    <a href="login.php">
        <i class="fa fa-sign-in"></i> 
        Login
    </a>
</p>

Fiddle using your example.

Kilian Stinson
  • 2,376
  • 28
  • 33
1

If you want to change all text sibling of i, select i tag in p and use Node.nextSibling property to selecting sibling text after element and change it.

$("p > a > i")[0].nextSibling.nodeValue = "Anmeldung";
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
    <a href="login.php">
        <i class="fa fa-sign-in"></i> 
        Login
    </a>
</p>

But if you want to replace part of text, use this code

$("p > a").html(function(i, html){
    return html.replace("Login", "Anmeldung");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
    <a href="login.php">
        <i class="fa fa-sign-in"></i> 
        Login
    </a>
</p>
Mohammad
  • 21,175
  • 15
  • 55
  • 84
0
<p>
  <a href="login.php">
    <i class="fa fa-sign-in"></i> 
    Login
  </a>
</p>

$('p').each(function() {
  $(this).find("a").text($(this).find("a").text.replace('Login', 'Anmeldung')); 
});
Munawir
  • 3,346
  • 9
  • 33
  • 51
Mustapha Larhrouch
  • 3,373
  • 3
  • 14
  • 28
0

You can try this DEMO LINK HERE

<p>
    <a href="login.php">
        <i class="fa fa-sign-in"></i> 
        Login
    </a>
</p>

JS..

$('p').each(function() {
    $(this).html($(this).html().replace('Login', 'Anmeldung')); 
});

http://jsfiddle.net/hnfRe/402/

Md Rahman
  • 1,812
  • 1
  • 14
  • 15
0

use html instead of text function. . .

$('p').html($(this).html().replace('Login', 'Anmeldung'));
Ravikiran kalal
  • 1,050
  • 8
  • 11