# calculate line and column of a text with JavaScript

**URL:** <https://forum.kirupa.com/t/calculate-line-and-column-of-a-text-with-javascript/656250>\
**Category:** programming\
**Created:** [January 11, 2023, 6:55pm UTC](https://forum.kirupa.com/t/calculate-line-and-column-of-a-text-with-javascript/656250 "2023-01-11T18:55:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![payback](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/payback/32/20328_2.png) [@payback](https://forum.kirupa.com/u/payback)\
**Post date:** [January 11, 2023, 6:55pm UTC](https://forum.kirupa.com/t/calculate-line-and-column-of-a-text-with-javascript/656250/1 "2023-01-11T18:55:42Z")

</div>

Hello Gentlemen,

I have several programs written in cobol and I have developed a web  
application called “Program Editor” to manipulate them.  
This application , among several functions , calculates the row and column when positioning the cursor on the desired instruction using the mouse.  
Program instructions are presented in a textarea field.  
It is normal to write a cobol program, starting statement, in different columns depending on the statement level.  
For example :  
_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\__  
FILE-CONTROL.  
_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\__  
COPY “C:\COBOL\WEB\ARQUIVO\CGIACRTO.SEL”.  
SELECT PRINT-CIA ASSIGN TO   
“C:\PAYBACK\CONTABIL\TABELAS\CONTRATO\X.JS”.  
This program excerpt is being presented correctly in the textarea field, that is

a- The character \* starts in column 7  
b- The F character of FILE-CONTROL starts on line 8  
c- The C character of COPY starts at column 8  
d- Character S of SELECT starts at column 8  
e- The " character of "c:\ starts on line 21

When clicking the mouse after the character " of ITEM E above, the application calculates column=8.  
Note - Notepad also shows column 8 however it shows in column 21.

This is due to the coding of the text editor that generated this program ( PWB which is very old )  
When pressing the key to move the cursor , it jumps from column 1 to column 9 and then to column 16.  
It looks like the PWB EDITOR has a special encoding for consecutive spaces that is correctly interpreted by the field TEXTAREA and NOTEPAD however when counting the characters it is not added correctly.  
Note - The line is being calculated correctly.

The Javascript that makes this count is:

function getPosCursor(form) {  
var tan = doGetCaretPosition(document.getElementById(‘text’));  
var row = 1;  
var column = 1;  
var element = document.editor.text.value;  
if ( tan \< 1 ) { return; }  
for (var i = 0; i \< tan; i++) {  
if (element.charAt(i) == “\n” || element.charAt(i) == “\r” ) {  
row = row + 1;  
column = 1;  
} else {  
column = column + 1;  
}  
}  
document.forms[0].txtLine.value = line;  
document.forms[0].txtColumn.value = column;  
}  
function doGetCaretPosition(ctrl) {  
var CaretPos = 0;  
// IE Support  
if (document.selection) {  
ctrl.focus();  
var Sel = document.selection.createRange();  
Sel.moveStart(‘character’, -ctrl.value.length);  
CaretPos = Sel.text.length;  
}  
// Firefox support  
else if (ctrl.selectionStart || ctrl.selectionStart == ‘0’)  
CaretPos = ctrl.selectionStart;  
return (CaretPos);  
}

Does anyone know how to solve this problem?  
Thank you for your attention,  
Kleber

Note - This text was translated by google.

---

<div class="post-metadata">

**Author:** ![payback](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/payback/32/20328_2.png) [@payback](https://forum.kirupa.com/u/payback)\
**Post date:** [January 20, 2023, 3:37pm UTC](https://forum.kirupa.com/t/calculate-line-and-column-of-a-text-with-javascript/656250/2 "2023-01-20T15:37:47Z")

</div>

After researching this issue on the internet, I managed to solve  
this problem by changing the getPosCursor function to :

function getPosCursor(form) {  
var tan = doGetCaretPosition(document.getElementById(‘texto’));  
var linha = 1;  
var coluna = 1;  
var especial = ’ ';  
/\* special character ¬ used for space compression in an 8-byte word \*/  
var resto = 0;  
var element = document.editor.texto.value;  
if ( tan \< 0 ) { return; }  
for (var i = 0; i \< tan; i++) {  
if (element.charAt(i) == “\n” || element.charAt(i) == “\r” ) {  
linha = linha + 1;  
coluna = 1;  
} else {  
if ( element.charAt(i) == especial ){  
resto=coluna%8;  
if ( resto == 0 ) { coluna += -8; }  
coluna = coluna + 9 - resto;  
} else {  
coluna = coluna + 1;  
}  
}  
}  
document.forms[0].txtLine.value = linha;  
document.forms[0].txtColumn.value = coluna;  
};

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 20, 2023, 4:10pm UTC](https://forum.kirupa.com/t/calculate-line-and-column-of-a-text-with-javascript/656250/3 "2023-01-20T16:10:44Z")

</div>

That sounds impressive and complicated! Do you have a screenshot of what the final app looks like? Seems like a great tool for you to make broadly available given how many Cobol-related posts cross my general news feed 😀
