Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Jspsych & jatos: Selecting one component from a list of components

edited September 2023 in JATOS


I have designed an experiment in jspsych in which every 100 max-diff items are allocated to one component added in jatos. I have therefore 100 components and i want to share them by one general multiple link so that each component is selected randomly from the remaining incomplete components. Each component must be done once. How can I do this?

This is the code

<!doctype html>



<script src="libs/jquery-3.1.1.min.js"></script>

<script src="jatos.js"></script>

<link rel="stylesheet" href="css/pure-release-0.6.0/pure-min.css">

<link rel="stylesheet" href="css/sort.css">



<p>سلام. از اینکه وقت ارزشمند خود را در اختیار ما گذاشته‌اید و ما را در این پژوهش برای توسعه و غنی سازی منابع زبان فارسی یاری می‌کنید، سپاسگزاریم. برای آشنایی با نحوه انجام آزمون دکمه ادامه را بزنید.</p>

<button id="continueButton" class="pure-button">Continue</button>


<script type="text/javascript">

const components = [



// ... add more components here



// Create an array to keep track of completed components

const completedComponents = [];

// Function to check if all components have been completed

function allComponentsCompleted() {

return completedComponents.length === components.length;


// Function to randomly choose one component

function chooseComponent() {

// Filter out completed components from the list

const remainingComponents = components.filter(component => !completedComponents.includes(component));

// Randomly choose one component from the remaining list

const chosenComponent = remainingComponents[Math.floor(Math.random() * remainingComponents.length)];

// Run the chosen component online in JATOS



// Check if all components have been completed

if (allComponentsCompleted()) {

// All components have been completed, do something else

} else {

// Choose a component and run it






  • Hi!

    I'm not sure I understand your experiment. First, when you are talking about 'components', are you talking about JATOS components or something else?

    Then I had a glimpse at your code and saw jatos.startStudyComponent. This function does not exist in jatos.js or is this something you defined yourself? If you want to start a certain (JATOS) component you might find jatos.startComponent or jatos.startComponentByPos useful.



  • Hi Kristian,

    Yes, I mean JATOS components. I want to have one introductory component in JATOS and 100 components to choose from randomly and only once: each component for one participant. I also want JATOS to keep track of completed components that have saved results, to make sure all the components are chosen. I didn't know how to do the coding, and have arrived at the above code through search, but it didn't work. I also looked at the sample experiment randomize order between workers, but I could not figure how to change it so that rather than randomizing the order of presentation, I randomize the selection of one component. Any pointer to a demo or code is highly appreciated.

  • edited September 2023


    Your code probably didn't work for a few different reasons. First, some of the functions you're calling don't exist.

    But also, the approach of having 100 different components seems really cumbersome. It means that you need to create each of these 100 components by hand in the JATOS GUI I guess, and manually indicate the name of each of the corresponding 100 different HTML files. If you wanted to change something in the script, you would need to do it individually for each of those 100 HTML files.

    So my first question: what is exactly the difference between these scripts? Is there a way that you could condense them all into a single JATOS component, with 100 possible different conditions? Is it just the order of stimuli you're presenting?

    Then, on to your question - to keep track of what a participants are doing, the best is to use the Batch Session Data. This recent post has an example code of how to do it for two conditions

    In our docs we have an example of how to counterbalance participants, directing them to different components (as you want to do). Search in this page for the study "randomize tasks between workers".

    You could use the same logic to use the batch session data to determine a condition, and instead of starting the corresponding component with the condition, you could:

    1. store the condition you want to test in the study session data
    2. always start the next component, but have an if statement to read the value of the corresponding condition in the study session data and run the corresponding version/stimulus order/etc

    Hope this helps


  • edited September 2023

    Hi Elisa and many thanks. Feeling very embarrassed, I should confess 😳 I didn't know how to set the condition so that each of the js files containing items would be selected each time the experiment is run, up to the time that no condition is left out. So I have created all the 300 components for the three experiments. Here is the code for each component:

        var demographic = {
          type: jsPsychSurveyText,
          questions: [
            {prompt: 'سن', name: 'age', required: true},
            {prompt: 'جنسیت', name: 'gender', required: true},
            {prompt: 'مدرک تحصیلی', name: 'degree', required: true},
            {prompt: 'زبان مادری', name: 'MotherTongue', required: true},
            {prompt: 'در اکثر اوقات از چه زبانی استفاده می‌کنید؟ ', name: 'FrequentLang', required: true},
            {prompt: 'توضیح درباره خود در صورت تمایل', name: 'َAny explanation'}, 
        var expstart = {
          type: jsPsychHtmlButtonResponse,
          <p style="font-size:22px;color:darkblue;">شروع آزمون</p>
          <p>اکنون برای شروع آزمون دکمه ادامه را بزنید.</p>
          choices:['<p style="font-size:22px;color:darkblue;">ادامـه</p>']
        // var arr = [];
        // while(arr.length < 100){
        // var r = Math.floor(Math.random() * test_stimuli.length) + 1;
        //   if(arr.indexOf(r) === -1) arr.push(r);
        // }
        for (i = 0 ; i <  test_stimuli.length; ++ i){
          var maxdiff = {
          type: jsPsychMaxdiff,
          trial_no: test_stimuli[i][0],
          alternatives: test_stimuli[i].slice(1),
          labels: ['سلطه‌پذیرترین','سلطه‌گرترین'],
          preamble: '<p> زیر ستون سلطه‌گرترین و سلطه‌پذیرترین کلماتی را انتخاب کنید که در شما بیشترین و کمترین سطح احساس سلطه و کنترل را ایجاد می‌کنند .  </p>',
          randomize_alternative_order: true,
          required: true
        var thanks = {
          type: jsPsychHtmlButtonResponse,
          stimulus: 'پایان آزمون',
          prompt: `
          <p>دکمه پایان را برای ذخیره پاسخ ها بزنید. از اینکه ما را در این پژوهش یاری کردید، سپاسگزاریم.</p>
          <p>در صورتی که تمایل دارید به ما کمک کنید، می‌توانید لینک پرسشنامه را با دوستان یا سایر افراد خانواده به اشتراک بگذارید.</p>
          <p>با آرزوی شادی و تندرستی و خدانگهدار</p>
          choices:['<p style="font-size:22px;color:darkblue;>پایان</p>']
        /* start the experiment */
        jatos.onLoad(() => {

  • Hi Fatemeh,

    (just a hint, you can maintain code format when you write here, it makes everything easier to read. I've formatted your message above)

    I didn't understand what the difference is between all your 100 or 300 versions. Is it only the line:

     alternatives: test_stimuli[i].slice(1),

    In that case, you would need to replace the number (1) that you coded with a variable. I can tell you how, but first need to be sure I understand what all your different versions differ on.

  • Hi Elisa,

    The only code in which the files are different is this and in thecsecon file it is 1.js and the third it is 3.js and so on:

     <script src="0.js"></script>

    Taken from the heading:

    <!DOCTYPE html>
        <title>Dominance experiment</title>
        <script src="jatos.js"></script>
        <script src="jspsych/jspsych.js"></script>
        <script src="jspsych/plugin-html-button-response.js"></script>
        <script src="jspsych/plugin-survey-text.js"></script>
        <script src="jspsych/plugin-maxdiff.js"></script>
        <script src="0.js"></script>
        <link href="jspsych/jspsych.css" rel="stylesheet" type="text/css" />
  • Hi Elisa,

    I went for the option of selecting files randomly rather than components and added this code to replace the commented code, but it didn't work either. The js file of the experiment with .txt extension is attached.

    <!-- <script src="1.js"></script> -->
        <script id="srcid" >
          var r = Math.floor(Math.random() * 100);
          var script = document.createElement('script');
          script.src = String(r) + ".js" ;
          console.log("script.src:", script);
          // Display the script.src value in a div element
  • Hi, sorry - I still don't understand :)

    What is the difference between the code in the scrips: 0.js and 1.js ?

  • Hi Elisa, The only difference is the name of js file as each js file contains 100 items

  • edited September 2023

    Hi Fatemeh,

    Sorry, with my question I meant to ask: What are you doing differently within those 100 different .js files? As I understand it each of those files calls different stimuli. Is there no way you can define that list of stimuli within your main js code in your script? I mean, instead of calling 100 different scrips, define an array of stimuli that you might want to load, and randomly select the element in the array? (If you don't know how to do it, that might be a question for jsPsych).

    If you really want to load those scripts depending on the condition, you could try loading scrips dynamically.

    But here, I really would ask jspsych. Now that we agree that having 100 different components is not the best alternative, it becomes a purely js-question, not a JATOS-question :)



Sign In or Register to comment.

agen judi bola , sportbook, casino, togel, number game, singapore, tangkas, basket, slot, poker, dominoqq, agen bola. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 50.000 ,- bonus cashback hingga 10% , diskon togel hingga 66% bisa bermain di android dan IOS kapanpun dan dimana pun. poker , bandarq , aduq, domino qq , dominobet. Semua permainan bisa dimainkan hanya dengan 1 ID. minimal deposit 10.000 ,- bonus turnover 0.5% dan bonus referral 20%. Bonus - bonus yang dihadirkan bisa terbilang cukup tinggi dan memuaskan, anda hanya perlu memasang pada situs yang memberikan bursa pasaran terbaik yaitu Bola168. Situs penyedia segala jenis permainan poker online kini semakin banyak ditemukan di Internet, salah satunya TahunQQ merupakan situs Agen Judi Domino66 Dan BandarQ Terpercaya yang mampu memberikan banyak provit bagi bettornya. Permainan Yang Di Sediakan Dewi365 Juga sangat banyak Dan menarik dan Peluang untuk memenangkan Taruhan Judi online ini juga sangat mudah . Mainkan Segera Taruhan Sportbook anda bersama Agen Judi Bola Bersama Dewi365 Kemenangan Anda Berapa pun akan Terbayarkan. Tersedia 9 macam permainan seru yang bisa kamu mainkan hanya di dalam 1 ID saja. Permainan seru yang tersedia seperti Poker, Domino QQ Dan juga BandarQ Online. Semuanya tersedia lengkap hanya di ABGQQ. Situs ABGQQ sangat mudah dimenangkan, kamu juga akan mendapatkan mega bonus dan setiap pemain berhak mendapatkan cashback mingguan. ABGQQ juga telah diakui sebagai Bandar Domino Online yang menjamin sistem FAIR PLAY disetiap permainan yang bisa dimainkan dengan deposit minimal hanya Rp.25.000. DEWI365 adalah Bandar Judi Bola Terpercaya & resmi dan terpercaya di indonesia. Situs judi bola ini menyediakan fasilitas bagi anda untuk dapat bermain memainkan permainan judi bola. Didalam situs ini memiliki berbagai permainan taruhan bola terlengkap seperti Sbobet, yang membuat DEWI365 menjadi situs judi bola terbaik dan terpercaya di Indonesia. Tentunya sebagai situs yang bertugas sebagai Bandar Poker Online pastinya akan berusaha untuk menjaga semua informasi dan keamanan yang terdapat di POKERQQ13. Kotakqq adalah situs Judi Poker Online Terpercayayang menyediakan 9 jenis permainan sakong online, dominoqq, domino99, bandarq, bandar ceme, aduq, poker online, bandar poker, balak66, perang baccarat, dan capsa susun. Dengan minimal deposit withdraw 15.000 Anda sudah bisa memainkan semua permaina pkv games di situs kami. Jackpot besar,Win rate tinggi, Fair play, PKV Games