I have a textarea and some buttons. Onclick of each button I have to do following:
- Check if textarea contains some text XXX.
- If contains then remov开发者_C百科e it.
- If not then add it.
How can I do this in javascript? I have tried following but it does not work:
function addRecip(con){
var myvalue = document.getElementById("textarea1").value;
if(myvalue.indexof(con+",")==-1){
document.getElementById("textarea1").value = myvalue + con + ",";
} else {
document.getElementById("textarea1").value = myvalue.replace(con + ",","");
}
}
indexof
is actually meant to be spelled indexOf
, and JavaScript is case-sensitive.
This works:
function addRecip(con){
var myvalue = document.getElementById("textarea1").value;
if(myvalue.indexOf(con+",")==-1){
document.getElementById("textarea1").value = myvalue + con + ",";
} else {
document.getElementById("textarea1").value = myvalue.replace(con + ",","");
}
}
精彩评论