I want to check if a string has a certain letter, and then remove said letter.
jsFiddle — http://jsfiddle.net/nNb5S/6/
For example, if you type...
- XhelloX
- YhelloY
- ZhelloZ
.. in the "input" textarea, the "result" should return...
- hello
- hello
- hello
I already figured it out, but I was wondering if it could be done via Switch?
This is my working code ::
function doSomething() {
var input = document.getElementById("h"),
result = document.getElementById("m");
var x = /x/gi;
var y = /y/gi;
var z = /z/gi;
if(input.value.indexOf(x)) {
result.value = input.value.replace(x,"");
}
if(input.value.indexOf(y)) {
result.value = result.value.replace(y,"");
}
if(input.value.indexOf(z)) {
result.value = result.value.replace(z,"");
}
}
This is my switch code ::
This below code ONLY works when arg is defined; but, I don't want to define it.
function doSomething() {
var input = document.getElementById("h"),
result = document.getElementById("m");
var x = /x/gi;
var y = /y/gi;
var z = /z/gi;
/*arg = x;*/ // ONLY WORKS WHEN YOU DEFINE arg. HOW DO YOU DEFINE arg AS ANY OF THE VARIABLES?
var iO = input.value.indexOf(arg);
switch (arg) {
case x:
result.value = input.value.replace(x, "");
break;
case y:
result.value = result.value.replace(y, "");
break;
case z:
result.value = result.value.replace(z, "");
break;
}
}
val.replace(/x|y|z/gi, '')/[xyz]/