Survey-Text-Plugin : Continue on "Enter"-KeyPress

346 views
Skip to first unread message

eduard...@gmail.com

unread,
Jun 15, 2015, 6:49:17 AM6/15/15
to jsp...@googlegroups.com
Hey there,

I tried some time to get the survey-text-plugin to do the same thing on pressing "Enter", as would be done when the subject clicked on the "Submit"-Button.
But due to my limited skills with JavaScript and Jquery, I can't get it to work at all.

Last thing I tried (in jspsych-survey-text.js) for example was trying to add an onkeydown event to the textbox:

$("#jspsych-survey-text-" + i).append('<input type="text" name="#jspsych-survey-text-response-' + i + '" onkeydown="runScript(event)"></input>');

function runScript(e){
if(e.keyCode == 13){
alert("pressed enter key"); // for testing
// it should do the exact same thing as
// $("#jspsych-survey-text-next").click
}

}

It only tells me that runScript is not defined.
So where would I need to put the function that is executed on click of the "Submit"-Button to work with "Enter"-KeyPress?

Thanks in advance,

Eduard

Josh de Leeuw

unread,
Jun 15, 2015, 9:52:16 AM6/15/15
to eduard...@gmail.com, jsp...@googlegroups.com
I think you've got the right idea if you only want the enter key to work when the text box is active. I'm not sure why you get a message that runScript is not defined. There are also a couple alternatives you could try. 

You could use jQuery to add the event to the input element after it is created:

$("#jspsych-survey-text-" + i).on('keydown', function(e) { runScript(e); });

Or you could use jsPsych's method for key listening and attach the event to the whole document (so pressing enter would fire the event regardless of whether they are typing in the text box or not).


--
You received this message because you are subscribed to the Google Groups "jsPsych" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jspsych+u...@googlegroups.com.
To post to this group, send email to jsp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jspsych/cd02b4be-c34f-4bb2-a5b5-67d4674b2a36%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

eduard...@gmail.com

unread,
Jun 17, 2015, 4:45:33 AM6/17/15
to jsp...@googlegroups.com, eduard...@gmail.com
Got it working with your help! Thank you very much once again!

Pedro Neto

unread,
Jan 4, 2019, 1:02:55 AM1/4/19
to jsPsych
Hi there,

Would you make part of your code available so I could try to figure it out also? I'm trying to do the same thing, only with the html-slider-response plugin (transfer the continue button's function to the spacebar).

Any help would be great!

Thanks,
P.N.

Reply all
Reply to author
Forward
0 new messages