1

I'm trying to replace text every time I've an input change.

Here's an example: https://jsfiddle.net/ewdp3Lnr/

$("[name='replacer']").on('change paste keyup', function() {
    PRO_Content = $('#content').html();
    PRO_Content = PRO_Content.replace('####', $("[name='replacer']").val());
    $('#content').html(PRO_Content);
});

It's working but only for the first change detected.

But not for the second, third, ...

Could you point me in the right direction?

Thanks.

1
  • your first value goes in a circle. here - PRO_Content = $('# content').html(); Commented Jan 22, 2021 at 14:30

2 Answers 2

1

Just get the content before any change and use the initial value. Don't rewrite the initial value because that will replace ####

var pattern = $('#content').html();
$("[name='replacer']").on('change paste keyup', function() {   
    PRO_Content = pattern.replace('####', $("[name='replacer']").val());
    $('#content').html(PRO_Content);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Put something here to replace the #### in the HTML code:<br>
<input type="text" name="replacer">

<hr>

<span id="content">
  <p>This man is <strong style="color:red">####</strong>.</p>
</span>

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

4 Comments

Thanks a lot. Last thing, if I have more than one '####' how can I proceed please?
@Ross you need to use replaceAll
For example: let i = 0; let changes = ["first", "second"]; '#### something else ####'.replaceAll('####', () =>{ return changes[i++];});
Hi @Ross if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this.
0

You can simplify your code using .text( function ).

Moreover, instead of change and keyup evets you can use the input event.

The snippet:

var element = $('#content :contains("####")').filter((i,e) => e.textContent == '####');
$("[name='replacer']").on('input paste', function(e) {
    element.text(function(idx, text) {
        return e.target.value;
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<input type="text" name="replacer">

<hr>

<span id="content">
  <p>This man is <strong style="color:red">####</strong>.</p>
</span>

2 Comments

Hello, it's woking but I'm looking for a more customizable way. Actually your code it too much based on the HTML structure and not on the key (i.e.: ####).
@Ross Snippet updated. Now I save the element where change the text content...... IN any case I don't see why use a replace. Let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.