how to get the text of a textfield ?

mnotermans5114 Community Member Posts: 916
Im working on a project where i need to get the text of a specific textfield onscreen in Lectora, and do stuff with it.

First step for me is getting the proper clean text.

And there an issue turns up. An issue with the formatting of text. Whenever a newline is entered in a Lectora textfield, it creates a span for it. So my single textfield in Lectora ends up in 5 spans with text. When getting the text with either JQuery .text(), innerText or Mike Wilcox amazing script getPlainText ( ) i get extra line breaks for each span, thus making my formatting in the Canvas looking off.

So does anyone has a good solution to get the text of a Lectora textfield without having to deal with the spans ??




  • mnotermans5114
    mnotermans5114 Community Member Posts: 916
    Often just describing an issue i run into, pops an idea in my brain how to fix the issue at hand. Again this time.

    So i had a textfield. Reading the text with Javascript in the described methods above resulted in extra linebreaks i wanted to avoid. Well reading it like this fixes that.

    var text2Use ="";
    var textSpans = document.getElementById("text9482").getElementsByTagName("span");
    for (var i = 0, l = textSpans.length; i < l; i++) {
    if(textSpans[i].textContent || textSpans[i].innerText != undefined){
    text2Use += textSpans[i].textContent || textSpans[i].innerText;

    Basically i loop through the Lectora generated spans in the Html and combine them into a new String to use...

  • sirwise23
    sirwise23 Community Member Posts: 442 ☆ Roadie ☆
    Don't you love it when you can answer you own question?

    Quick question. What kind of text box are you using to grab text from? Because if you use an Entry Field box from the Test & Survey menu, it creates a variable with the text so that you don't have to take the straight text from the box. Like if I go in there now and create an entry box, it creates a variable named Entry_0001. If you grab the text from that variable I don't think it will have the spans in it.

    Not sure what it is you are trying to grab and my JavaScript knowledge is pretty bad but I've found that there is almost always a work around of sorts.
  • mnotermans5114
    mnotermans5114 Community Member Posts: 916
    Hi Lazaro,

    True with input fields. But in this case its just a plain old textfield. With enters for paragraphs, and there you get the spanification ;-)

    And as you state, indeed almost anything in Lectora can be done in at least 2 or 3 ways, but in this case plain text fields are a must.