Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Code doesn't work

More
11 months 3 weeks ago #272767 by oscar_duran_v
Please help us help you and fill where relevant:
LimeSurvey version: [see right hand bottom of your LimeSurvey admin screen]
Own server or LimeSurvey Cloud:
Survey theme/template:
==================
(Write here your question/remark)Please help us help you and fill where relevant:
LimeSurvey version: [see right hand bottom of your LimeSurvey admin screen]
Own server or LimeSurvey Cloud:
Survey theme/template:
==================
(Write here your question/remark)Please help us help you and fill where relevant:
LimeSurvey version:  Versión 6.13.2+250506
Own server or LimeSurvey Cloud: Own server
Survey theme/template: Bootswtch Template  

Attachment not found


==================
Please let me know why the next code doesn't work in my script question content:

/**
 * Guarda un valor en un campo de texto de LimeSurvey
 * y lo oculta si se desea.
 *
 * @param {string} questionCode - El código de la pregunta (ej. "P0" o "codigoCuestionario")
 * @param {string} value - El valor que se desea guardar
 * @param {boolean} hide - true para ocultar el campo al usuario
 */
 
function setAnswerValue(questionCode, value, hide = true) {
    const $campo = $('#answer' + questionCode);

    if ($campo.length) {
        $campo.val(value)
              .prop("readonly", true)
              .trigger("input")
              .trigger("keyup")
              .trigger("change");

        if (hide) {
            $campo.hide();
        }
    } else {
        console.warn("No se encontró el campo con código: " + questionCode);
    }
}

// ====== Ejemplo de uso ======
$(document).ready(function () {
    const ahora = new Date();
    const folio = ("0" + ahora.getDate()).slice(-2)
        + ("0" + (ahora.getMonth() + 1)).slice(-2)
        + ahora.getFullYear()
        + "-"
        + ("0" + ahora.getHours()).slice(-2)
        + ("0" + ahora.getMinutes()).slice(-2);

    // Guardar el folio en la pregunta "P0"
    setAnswerValue("0", folio, true);

    // Mostrarlo en un lugar visible
    $("#quizNumberDisplay").text(folio);
});

//Su número de solicitud es: <span id="quizNumberDisplay"></span>

Please Log in to join the conversation.

More
11 months 2 weeks ago #272787 by tpartner
Replied by tpartner on topic Code doesn't work
Are you seeing any errors in the console?

I doubt you have a question with code "0". Also, the ID of a text input contains the full SGQ, something like "answer11111X222X333".

So, assuming that this is in the question that you want to populate, try this:

Code:
/** * Guarda un valor en un campo de texto de LimeSurvey * y lo oculta si se desea. * * @param {string} questionCode - El código de la pregunta (ej. "P0" o "codigoCuestionario") * @param {string} value - El valor que se desea guardar * @param {boolean} hide - true para ocultar el campo al usuario */ function setAnswerValue(questionCode, value, hide = true) { const $campo = $('#answer' + questionCode); console.log($campo); if ($campo.length) { $campo.val(value) .prop("readonly", true) .trigger("input") .trigger("keyup") .trigger("change"); if (hide) { $campo.hide(); } } else { console.warn("No se encontró el campo con código: " + questionCode); } } // ====== Ejemplo de uso ====== $(document).ready(function () { const ahora = new Date(); const folio = ("0" + ahora.getDate()).slice(-2) + ("0" + (ahora.getMonth() + 1)).slice(-2) + ahora.getFullYear() + "-" + ("0" + ahora.getHours()).slice(-2) + ("0" + ahora.getMinutes()).slice(-2); // Guardar el folio en la pregunta "P0" setAnswerValue("{SGQ}", folio, true); // Mostrarlo en un lugar visible $("#quizNumberDisplay").text(folio); });

Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.
The following user(s) said Thank You: DenisChenu

Please Log in to join the conversation.

Moderators: holch, tpartner

Lime-years ahead

Online-surveys for every purse and purpose