Storing the line number of all the occurrence of particular character in javascript

允我心安 提交于 2019-12-01 11:41:28

Is this something you want to do:

<textarea rows="10" cols="100" id="editor" onkeypress="hello(event);" ></textarea>
<div id="lineNo"></div>
<script>
  var arr = [];
  var c = 0;
  function hello(e) {
    var keyp=e.charCode; 
    if(keyp=='123') {
      arr[c++] = getLineNumber();
    }

    document.getElementById("lineNo").innerHTML=
      "Array content is...   "+ arr.join(' ');      
  }
  
  function getLineNumber() {
    var ta=document.getElementById("editor")
    var x=ta.value.substr(0, ta.selectionStart).split("\n").length;
    console.log(x)
    return x;
  }
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!