I want to change the html of a text field by code (in order to change the link according to other parameters) but it doesn’t works, and i get a weird comportment !
I’ve done a simple test with a simple text field and a button. In this sample, the purpose of the button is not really to change the html, but replacing the html by itself doing a simple equal (“=”) to show the problem.
What i really want to achieve is to dynamically change the link within a text field. In my website, i have calculated a new link and after replacing it in the .html property of my text field, the new link works fine, but after a few occurences, it doesn’t ! Debugging this, i find that the content of the .html property of my text field was growing. Then, i made this simple example code to show this issue.
****
In fact, this issue is not on the $w’(“#Html”), but on the .html value of $w(“#MyTextField”). $w’(“#Html”) field is here only to show the html content of MyTextField.
Each time i Change some part of the $w(“#MyTextField”).html, the word “wixui-rich-text__text” (which is within the $w(“#MyTextField”).html) is automatically doubled ! In this example, i’ve just affected the same value to $w(“#MyTextField”).html, doing $w(“#MyTextField”).html=$w(“#MyTextField”).html, which normally does nothing !
The pictures 2, 3 and 4 are showing this : Each clic on the button makes the contains of the $w(“#MyTextField”) .html property growing …
That’s an interesting issue you’ve mentioned. I’ve faced something similar when trying to dynamically update text fields in Wix.
In my case, the problem was related to the element ID not being correctly targeted in the code.
Make sure you’re using the correct selector and that the text field is fully loaded before executing the code.
You can try wrapping your function inside the $w.onReady() block — it often fixes such timing issues.
This line of code replace effectively www.youtube.com by www.sony.com BUT, in the .html of the text field, all mentions to “wixui-rich-text__text” are changed too, replaced by“wixui-rich-text__text wixui-rich-text__text”.
I’ve tried to explain this to you on the picture below
Yeah, after testing it, that’s exactly what happens. I’m not entirely sure why this process is necessary, but with the help of AI, I came up with the simplest possible solution. I usually avoid using while loops because they can be risky in some cases, but this time it turned out to be really useful, so I decided to go with the while approach. I think this should successfully remove the duplicates.
$w('#button1').onClick(() => {
let html = $w("#text1").html;
html = html.replace("sony.com", "amazon.co.jp");
while (html.includes('wixui-rich-text__text wixui-rich-text__text'))
html = html.replace('wixui-rich-text__text wixui-rich-text__text', 'wixui-rich-text__text');
$w("#text1").html = html;
});