I am trying to implement a function in JavaScript that gives me an output like this for a given input value
Input: stack overflow
Output: Stack_Overflow
Input: the big bang theory
Output: The_Big_Bang_Theory
I have written the code to capitalize the letters but cannot seem to figure how to call both the functions on the same input at the same time. I am relatively new to Javascript and any help would be greatly appreciated. I will share my code here for further clarity
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<body>
<input id="myInput" type="text" value="" size="50" />
<pre id="myOutput" type="myInput">type something in the box above</pre>
<script>
String.prototype.capitalize = function(){
return this.toLowerCase().replace( /\b\w/g, function (m) {
return m.toUpperCase();
});
};
String.prototype.replaceAll = function(){
if(!search || !replace){return this;}
return this.replace(/ /g,"_"), function (n){
return n;
});
};
var myInput = document.getElementById('myInput');
var myOutput = document.getElementById('myOutput')
myInput.addEventListener('input', function(e) {
myOutput.innerHTML = this.value.capitalize();
});
myInput.addEventListener('input', function(f)) {
myOutput.innerHTML = this.value.replaceAll();
});
</script>
</body>
</html>