I want to count the number of characters in the text area per line.

Asked 1 years ago, Updated 1 years ago, 32 views

I'm stuttering at the text counter in the text area.I would appreciate your help.

First of all, regarding the conditions,

1—Counts half-width as 0.5 and full-width as 1.0
2—Line feed, space not counted
3—Count line by line until line 3 (ignore line 4 and later)

Current Issues
Half-width and full-width decisions seem to be ignored, and even half-width ones count as 1.0.

That's all.

functionShowLength(str){
  varlen = 0;

// Character size check
  for(i=0;i<str.length;i++){
    varc = str.charCodeAt(i);
  
    if((c>=0x0&c<0x81)||(c==0xf8f0)||(c>=0xff61&c<0xffa0)||(c>=0xf8f1&c<0xf8f4){
      len+ = 0.5;
    }
    else {len+=1;}
  }
  

// Split
  vararr=str.split(/\r\n|\r|\n/);
  
  for(i=0;i<arr.length;i++){
//    For log verification
    console.log("arr["+i+"] has a character count of "+arr[i].length+"");
  }
  
  
  
    ShowLength.innerHTML = len.toFixed(1);

    document.getElementById("inputlength01").innerHTML =arr[0].length;
    document.getElementById("inputlength02").innerHTML =arr[1].length;
    document.getElementById("inputlength03").innerHTML =arr[2].length;
}
<body>
  <table>
    <tr>
    <th>Line-by-line counters</th>
    <td>
      <textarea id="input_text"placeholder=""name="summary" rows="5" onkeyup="ShowLength(this.value,'inputlength');">/textarea>
        <div class="countWrrap">
          <div class="countDsign">
            <ul>
              <liid="comment01">Line 1:<spanid="inputlength01">0.0</span>/35<li>;
              <liid="comment02">Line 2:<spanid="inputlength02">0.0</span>/35<li>
              <liid="comment03">Line 3:<spanid="inputlength03">0.0</span>/35<li>;
            </ul>
          </div>
        </div>
      </td>
    </tr>
  </table>
</body>

javascript html jquery

2022-09-29 22:50

1 Answers

 document.getElementById("inputlength01").innerHTML =arr[0].length;

I ended up using arr[0].length, so

varlen=0;

// Character size check
for(i=0;i<str.length;i++){
  varc = str.charCodeAt(i);

  if((c>=0x0&c<0x81)||(c==0xf8f0)||(c>=0xff61&c<0xffa0)||(c>=0xf8f1&c<0xf8f4){
    len+ = 0.5;
  }
  else {len+=1;}
}

The length len calculated in means nothing.

One more thing,

ShowLength.innerHTML=len.toFixed(1);

I don't think is meaningful. ShowLength is a function, so if you put a value in that property, nothing happens on the screen.

Finally, element.innerHTML parses the substituted string as HTML, which is useless.We recommend that you use element.textContent instead.


2022-09-29 22:50

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.