# Word Count in text box input

**URL:** <https://forum.wixstudio.com/t/word-count-in-text-box-input/9589>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [September 8, 2021, 6:51pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589 "2021-09-08T18:51:47Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![imagineimage](https://avatars.discourse-cdn.com/v4/letter/i/53a042/32.png) [@imagineimage](https://forum.wixstudio.com/u/imagineimage)\
**Post date:** [September 8, 2021, 6:51pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/1 "2021-09-08T18:51:47Z")

</div>

Hopefully someone can help with this wonky issue. I have the code working on a text box input textBox 1 and reporting to a text box text107. But I cannot replicate this code on another page or even to a different location on the same page. Here is the code that works.

export function textBox1\_change\_1 () {  
const remainingLength = $w ( ‘#textBox1’ ). maxLength - $w ( ‘#textBox1’ ). value . length ;  
$w ( ‘#text107’ ). text = `Only ${ remainingLength } remaining characters.` ;  
}

---

<div class="post-metadata">

**Author:** ![imagineimage](https://avatars.discourse-cdn.com/v4/letter/i/53a042/32.png) [@imagineimage](https://forum.wixstudio.com/u/imagineimage)\
**Post date:** [September 8, 2021, 6:58pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/2 "2021-09-08T18:58:04Z")

</div>

Note that I assume one must Set Character Limit in the setup options (the gear icon) for the text box.

![](https://us1.discourse-cdn.com/wix/original/3X/e/0/e03e8b0ead64c59409f1456cd567f2c1da6263c8.png)

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [September 8, 2021, 7:26pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/3 "2021-09-08T19:26:39Z")

</div>

Whats exactly the problem with the REPLICATION?

You have a code which counts → remaining-characters (remaining-length) of a STRING-TEXT.

Everytime the Input-Textbox is changed, the remaining characters also changes.

Here an example, if you would want to show the results in 3 different textfields.

```javascript
export function textBox1_change_1() {
    const remainingLength=$w('#textBox1').maxLength-$w('#textBox1').value.length;
    $w('#text107').text = `Only ${remainingLength} remaining characters.`;
    $w('#text108').text = `Only ${remainingLength} remaining characters.`;
    $w('#text109').text = `Only ${remainingLength} remaining characters.`;
}

```

here an example where you have a second INPUT…

```javascript
export function textBox2_change_1() {
    const remainingLength=$w('#textBox1').maxLength-$w('#textBox1').value.length;
    $w('#text110').text = `Only ${remainingLength} remaining characters.`;
}

```

Or another version…

```javascript
$w.onReady(async()=>{

  $w('#textBox1').onChange(()=>{
    const remainingLength=$w('#textBox1').maxLength-$w('#textBox1').value.length;
    $w('#text110').text=`Only ${remainingLength} remaining characters.`;
  });
  
  $w('#textBox2').onChange(()=>{
    const remainingLength=$w('#textBox2').maxLength-$w('#textBox2').value.length;
    $w('#text111').text=`Only ${remainingLength} remaining characters.`;
  });
  
});

```

---

<div class="post-metadata">

**Author:** ![imagineimage](https://avatars.discourse-cdn.com/v4/letter/i/53a042/32.png) [@imagineimage](https://forum.wixstudio.com/u/imagineimage)\
**Post date:** [September 9, 2021, 2:30pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/4 "2021-09-09T14:30:16Z")

</div>

The issue is putting this code an another page doesnt work; attaching this code to a second or more instance on the page doesnt work; only the first instance works.

---

<div class="post-metadata">

**Author:** ![imagineimage](https://avatars.discourse-cdn.com/v4/letter/i/53a042/32.png) [@imagineimage](https://forum.wixstudio.com/u/imagineimage)\
**Post date:** [September 9, 2021, 2:31pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/5 "2021-09-09T14:31:09Z")

</div>

My suspicion is the KIND of text box input may affect proper function?

---

<div class="post-metadata">

**Author:** ![bwprado](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/bwprado/32/7220_2.png) [@bwprado](https://forum.wixstudio.com/u/bwprado)\
**Post date:** [September 9, 2021, 4:11pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/6 "2021-09-09T16:11:49Z")

</div>

You have to have the same elements (including their IDs) as the original page, otherwise, it is not going to work.

---

<div class="post-metadata">

**Author:** ![imagineimage](https://avatars.discourse-cdn.com/v4/letter/i/53a042/32.png) [@imagineimage](https://forum.wixstudio.com/u/imagineimage)\
**Post date:** [September 9, 2021, 8:30pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/7 "2021-09-09T20:30:18Z")

</div>

Not sure I understand this. Same ids as original page?

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [September 9, 2021, 8:36pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/8 "2021-09-09T20:36:58Z")

</div>

@imagineimage  
What IDs do have all your used elements on the related page?  
You have to edit the IDs with your own.

The best way to solve your issue quickly and without making any mytery, just show your whole page-setup on a screenshot and all related element-IDs.

1. You have a → text-box? → what is the ID of it?
2. You have a button? → take a look on the ID of that element

check all your used element-IDs and replace all the ID in the given code-example with your own.

Show also your current working CODE (which works/or not works).

We do not sit in front of your PC → we can not see your CODE and PAGE-SETUP 😉 and you do not give any feedback.

What have you tried already? What exactly do not work? Which error do you get in the CONSOLE ?

---

<div class="post-metadata">

**Author:** ![imagineimage](https://avatars.discourse-cdn.com/v4/letter/i/53a042/32.png) [@imagineimage](https://forum.wixstudio.com/u/imagineimage)\
**Post date:** [September 9, 2021, 8:40pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/9 "2021-09-09T20:40:27Z")

</div>

It appears this fix above has worked. All others failed.

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [September 9, 2021, 9:00pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/10 "2021-09-09T21:00:03Z")

</div>

Hi there …

The event you need to use is the [**onInput( )**](https://www.wix.com/velo/reference/$w/textbox/oninput) , [**onChange( )**](https://www.wix.com/velo/reference/$w/textbox/onchange) only runs once the input field loses focus.

```javascript
const field = $w('#inputBox');

field.onInput(event => {
    const charsLeft = field.maxLength - event.target.value.length;
    
    if (charsLeft === 0) {
        $w('#chars').text = 'No characters left.'
    } else {        
        $w('#chars').text = `Only ${charsLeft} characters remaining.`
    }    
})

```

Hope this helps~!  
Ahmad

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [September 9, 2021, 9:01pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/11 "2021-09-09T21:01:58Z")

</div>

Ok, did not pay attention on that 🙄🙄🙄

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [September 9, 2021, 9:03pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/12 "2021-09-09T21:03:17Z")

</div>

@russian-dima you ask tons of questions at once, I feel like being questioned by an officer 😂 Even I won’t be able to answer them all.

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [September 9, 2021, 9:08pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/13 "2021-09-09T21:08:05Z")

</div>

@russian-dima little details can save you hours if not days of debugging

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [September 9, 2021, 9:25pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/14 "2021-09-09T21:25:47Z")

</div>

@ahmadnasriya

I totaly agree!

---

<div class="post-metadata">

**Author:** ![anon72313111](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@anon72313111](https://forum.wixstudio.com/u/anon72313111)\
**Post date:** [September 10, 2021, 11:09am UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/15 "2021-09-10T11:09:07Z")

</div>

😉

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [September 10, 2021, 12:08pm UTC](https://forum.wixstudio.com/t/word-count-in-text-box-input/9589/16 "2021-09-10T12:08:46Z")

</div>

@ahmadnasriya  
Also questions can act like answers 😜
