Tuesday, August 6, 2019

[JavaScript][Resolved] Add and remove items seprate by comma in textbox

You can use it for make tags, support old broswer :

<input type="text" id="st" name="st" value="sg1,sg2" />
<script>
var stNode = document.getElementById("st");
var st = stNode.value;
function addStrListItem(item){
  if(st.indexOf(item) == -1){
    st += ","+item;
    stNode.value = st;
  }
}
function removeStrListItem(item){
  if(st.indexOf(item) > -1){
    st = st.slice(1,st.length);
  }
  stNode.value = st;
}
addStrListItem("st3");
addStrListItem("st4");
addStrListItem("st5");
addStrListItem("st6");
removeStrListItem("st4");
removeStrListItem("st5");
removeStrListItem("st1");
</script>
Result:

Reference:
https://dev.to/adroitcoder/includes-vs-indexof-in-javascript

No comments :

Post a Comment