Reputation: 49
I am a total noob to javascript and need some direction.
I am trying to do the following:
Create 3 text boxes:
Button – calls a function to perform the replacement and displays the results in all uppercase.
I am using document.querySelector("myID").value;
to grab the value entered, but i am not sure if i should be using replace()
for this. Or if the variable needs to be converted to something else. (very confused)
If anyone could point me in the right direction it would be much appreciated.
Upvotes: 0
Views: 81
Reputation: 21
You could use something like this:
<body>
<textarea id="textArea" ></textarea><br>
<input id="input1" /><br>
<input id="input2" /><br>
<input type="button" value="Replace" onclick="doReplace()" />
<div id="output">OUTPUT!</div>
<script>
function doReplace() {
var getById = document.getElementById.bind(document);
var get = function(id) { return getById(id).value };
var text = get('textArea'),
input1 = get('input1'),
input2 = get('input2'),
div = getById('output');
var regex = new RegExp(input1, "g"); // to replace globally you need this!
div.innerHTML = text.replace(regex, input2).toUpperCase();
}
</script>
</body>
EDIT:
Upvotes: 1
Reputation: 2102
Javascript by default recognizes everything as string, when given a string operation. In this case you could use replace()
method directly without any conversions.
Upvotes: 0