Character Count using javascript
June 4, 2013 12:00 AM
I am trying to create a character count for my entry fields inside my Lectora title using javascript.
I am fairly new to javascript and am having trouble figuring exactly how to write the code to function properly.
This topic was covered during a breakout session at the User Conference last month, but my notes don't seem to be helping me as much as desired. If anyone has any suggestions or resources, I would greatly appreciate it.
Thanks!
Discussion (17)
Thank you for your input. I managed to get the character count working properly, but now I've run into another problem. The initial value of the entry field is 'empty' in Lectora, but ~~~null~~~ in the script, making the initial value of the entry field equal 10. Does you have any suggestions on how to clear this initial value?
Add a textfield, add an action to it "On Select/Change" --> Run JavaScript, then add following JS:
var a = VarEntry_0001.value.length;
console.log(a);
If you know anything about JS, you'll be able to repurpose it to do what you want.
Also, be aware: in Lectora, empty field variable contains "~~~null~~~", that's 10 characters.
Sample: https://dl.dropboxusercontent.com/u/11433463/trivantis/entrysample/entrysample.awt
Check if it equals '~~~null~~~' and count it as zero chars.
TMcCartney
Did you manage to get this to work?
I wondered if you might be able to share with me how you did this as I am really new to JS and I have added the JS to run when the entry field is selected but then I am lost from this point on.
I looked at the link but am still having trouble trying to figure this out.
If either of you T or Sergey could help out I would really appreciate it.
Thanks.
Here's a way I tested successfully:
1. Create a variable, e.g. length
2. Add 2 actions to your entry field:
On: Change / Select
Action: Run Javascript
Javascript:
Varlength.set(VarEntry_0001.value.length);
On: Change / Select
Action: Modify variable
Target: length
Type: Set equal to
Value: 0
Condition: Only if Entry_0001 is empty
Tim
console.log(a) just prints out the variable to the browser's console
if you don't know JS, just use Tim's method in the previous message.
@krissy 55960 wrote:
Thanks Tim
I am actually stuck with the JavaScript bit
I don't know a great deal about JS so am stuck with what I am supposed to do with this as I don't understand the console.log(a) bit. I understand that var a is going to be set by the length of the entry field 0001 but the rest is a bit beyond me.
Can you help with that bit?
Thanks a lot!
Thanks Tim
I am actually stuck with the JavaScript bit
var a = VarEntry_0001.value.length;
console.log(a);
If you know anything about JS, you'll be able to repurpose it to do what you want.
I don't know a great deal about JS so am stuck with what I am supposed to do with this as I don't understand the console.log(a) bit. I understand that var a is going to be set by the length of the entry field 0001 but the rest is a bit beyond me.
Can you help with that bit?
Thanks a lot!
Thanks Tim
I am actually stuck with the JavaScript bit
@ssneg 50811 wrote:
var a = VarEntry_0001.value.length;
console.log(a);
If you know anything about JS, you'll be able to repurpose it to do what you want.
I don't know a great deal about JS so am stuck with what I am supposed to do with this as I don't understand the console.log(a) bit. I understand that var a is going to be set by the length of the entry field 0001 but the rest is a bit beyond me.
Can you help with that bit?
Thanks a lot!
Sorry, i'm no javascript expert. Although it's probably basic stuff i don't know what it does either.
The way i proposed will update the lectora variable "length" with the length of the string in the entry field each time the field is selected or the entry is edited.
Tim
I am trying to create a character count for my entry fields inside my Lectora title using javascript just as the individual was above. I tried both methods in Lectora and so far it hasn't worked because my Lectora won't/can't find var Entry_001 after I have set up the javascript. Has the set up changed for the newer versions of Lectora? I'm currently using Lectora 16. If so, can someone help me with the right javascript.
Since I can't upload the files, here's the link to the student notes feature one I was trying to upload,
http://community.trivantis.com/shared-content/student-notes-feature-example/
I just adding a few more buttons and actions. I need a button (acts as a save function and will have other functions and actions) to popup a window once the text box (entry field) has more than 17 chars. If it has less than 17 characters when the user clicks the button then there should be a pop-up that they don't meet the min. requirement.
I found several ways to get Lectora to count the characters but, for some reason it is not processing to where I can use it as a condition (in a save/pop-up button)... which has me confused because if it can process the characters enough through javascript to send it to other actions, variables, conditions, and used as part of displayed text; it should be able to process it to a button.
undefined
Wasn't sure if you wanted the trigger to occur when the 17th character was entered or upon clicking the submit button so I did it both ways. There's minimal JavaScript and if this works for you I have no problem helping you get it going in your project. Also, since I did not hear back regarding spaces and punctuation, they both count towards the minimum.
Hope this helps.
Darrel
undefined
Hi Latresha. Are you looking to include or exclude spaces and punctuation in your count?
Sorry I didn't reply quickly. I needed it to include spaces and punctuation in my count, as was on the 17th character but through the submit button. So what you sent me was exactly what I trying to do, so thank you so much!
Discussions have been disabled for this post