开发者

Replace/Add string in textarea in js [closed]

开发者 https://www.devze.com 2023-03-24 17:51 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

I have a textarea and some buttons. Onclick of each button I have to do following:

  1. Check if textarea contains some text XXX.
  2. If contains then remov开发者_C百科e it.
  3. 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 + ",","");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消