How to include javascript generated text into html link?
Date : March 29 2020, 07:55 AM
this one helps. I have a javascript that displays a generated text into a div: document.getElementById('phrase').innerHTML = phrase; PHRASE_TEXT_GETS_SHOWN_HERE , Do you mean like: function setPhrase(phrase) {
var url = 'http://twitter.com/home?status=' + encode(phrase);
$('#phrase').html('<a href="' + url + '">' + phrase + '</a>');
}
function setPhrase(phrase) {
var url = 'http://twitter.com/home?status=' + encodeURI(phrase);
document.getElementById('phrase').innerHTML = '<a href="' + url + '">' + phrase + '</a>';
}
|
Replace html placeholder text with html element using javascript
Date : March 29 2020, 07:55 AM
will be helpful for those in need found the answer in chat: as the tags are parsed by some server side plugin, they were not rendered after writing them in client side JS. so replacing works fine, but plugin didn't...
|
Replace text with html formatting using text location (text span) in Javascript
Date : March 29 2020, 07:55 AM
will help you This uses regular expressions to get the job done. The dictionary is processed in reverse order so that the indexes for replacements won't change. var str = "Make 'this' become blue and also 'that'."
// color_dict contains the START of the span and the LENGTH of the word.
// i.e. this & that are both size 4.
var color_dict = { 6: "4", 34: "4" };
// get keys sorted numerically
var keys = Object.keys(color_dict).sort(function(a, b) {return a - b;});
// process keys in reverse order
for (var i = keys.length - 1; i > -1; i--) {
var key = keys[i];
str = str.replace(new RegExp("^(.{" + key + "})(.{" + color_dict[key] + "})"), "$1<font color='blue'>$2</font>");
}
document.write(str);
|
Replace text inside HTML leaving other HTML intact with vanilla javascript
Date : March 29 2020, 07:55 AM
this will help replace does not mutate the string, it returns a new one. Strings are immutable. var text = document.getElementById('prompt');
text.textContent = text.textContent.replace("Passed", "Completed");
document.getElementById('prompt').childNodes[0].textContent = "Completed";
|
How to Javascript to replace HTML text with new text when the HTML may have children elements
Date : March 29 2020, 07:55 AM
help you fix your problem You might remove all child spans and then check the textContent to ignore the rest of the markup (like s), capturing the step digit and replacing with that surrounded by and :document.querySelectorAll('p').forEach((p) => {
p.querySelectorAll('span').forEach(span => span.remove());
p.innerHTML = p.textContent.replace(/Step +(\d+)/g, '<b>Step $1</b>');
});
<p id="FirstPara" class=firstpara>This is a header</p>
<p class=firstpara>This is a reference to Step <span lang=HE>‎ </span><b>1</b>.</p>
<p class=firstpara>This is a reference to Step <span lang=HE>‎</span>2.</p>
<p class=firstpara>This is a reference to Step <span lang=HE>‎</span>1 and Step <span lang=HE>‎</span>2.</p>
document.querySelectorAll('p').forEach((p) => {
p.querySelectorAll('span[lang="HE"]').forEach(span => span.remove());
p.innerHTML = p.textContent.replace(/Step +(\d+)/g, '<b>Step $1</b>');
});
<p class=firstpara>This is a <span>reference</span> to Step <span lang=HE>‎ </span><b>1</b>.</p>
|