<input type="text" id="st" name="st" value="sg1,sg2" />Result:
<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>
Reference:https://dev.to/adroitcoder/includes-vs-indexof-in-javascript
No comments :
Post a Comment