Forum Moderators: open
<!DOCTYPE html>
<html>
<body>
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
<p id="demo">http://www.site.org/111/info/q4md-fft</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace("http://", "");
document.getElementById("demo").innerHTML = res;
document.getElementById('demo').value = res;
document.write(''+res+''+res2+'');
}
</script>
</body>
</html>
<p>Click the button to replace "Microsoft" with "W3Schools" in the paragraph below:</p>
<p id="demo">http://www.site.org/111/info/q4md-fft</p>
<button id="demo-btn">Try it</button>
var button = document.getElementById("demo-btn"); //select the button
button.addEventListener('click', function() { //added event listener to listen for click
var demo = document.getElementById("demo"), //added a var "demo" to reduce typing and make more readable
str = demo.textContent, //change innerHTML to textContent because you only want the text
res = str.replace("http://", ""); //no change here
demo.innerHTML = res;//no change here except using demo variable
//at this point the functions does what it should
//document.getElementById('demo').value = res; //I don't know what this is supposed to do?
//document.write(''+res+''+res2+'');//res2 is a variable that doesn't exist. (same as above)
});