I am attempting to write a survey which will provide experimental vignettes to participants based on previous ratings of political parties. Obviously, this means that I'll need to pipe results from previous questions to a new question. I've tried to do this with a matrix table, but that has led to the following error:
SE API Error: TypeError: Cannot read properties of null (reading 'val')
at A.i.eval (eval at <anonymous> (jfeLib.fa2484b7040929bdf0ac.min.js:2:125561), <anonymous>:25:56)
at A.i.<anonymous> (jsApi.8c8dd06a862b12e2d92e.min.js:1:5034)
at A.i.r (jfeLib.fa2484b7040929bdf0ac.min.js:2:29148)
at A.i.s (jfeLib.fa2484b7040929bdf0ac.min.js:2:29191)
at s (jfeLib.fa2484b7040929bdf0ac.min.js:2:28405)
at A.i._trigger (jfeLib.fa2484b7040929bdf0ac.min.js:2:30008)
at A.i.r [as _trigger] (jfe.e7b329a67c066949aeb4.min.js:2:368934)
at A.i._finishRenderAndTriggerReady (jfe.e7b329a67c066949aeb4.min.js:2:283139)
at A.i.r [as _finishRenderAndTriggerReady] (jfe.e7b329a67c066949aeb4.min.js:2:368934)
at A.i.__postRender (jfe.e7b329a67c066949aeb4.min.js:2:263578) ƒ () { var matrixQuestionID = "QR~QID22~";
var partyNames = [
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G"
];
var ratings = [];
For reference, this is the code I've written in the matrix table JavaScript: Qualtrics.SurveyEngine.addOnUnload(function()
Qualtrics.SurveyEngine.addOnUnload(function()
{
var matrixQuestionID = "QR~QID22~";
var partyNames = [
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G"
];
var ratings = [];
// Capture Ratings
for (var i = 0; i < partyNames.length; i++) {
var rating = parseInt($(matrixQuestionID+(i+1)).val());
ratings.push({ party: partyNames[i], rating: rating });
}
// Sort ratings
ratings.sort(function(a, b) {
return b.rating - a.rating;
});
// Store ordered parties
for (var j = 0; j < ratings.length; j++) {
Qualtrics.SurveyEngine.setEmbeddedData("SortedParty_" + (j + 1), ratings[j].party);
}
});
I am attempting to write a survey which will provide experimental vignettes to participants based on previous ratings of political parties. Obviously, this means that I'll need to pipe results from previous questions to a new question. I've tried to do this with a matrix table, but that has led to the following error:
SE API Error: TypeError: Cannot read properties of null (reading 'val')
at A.i.eval (eval at <anonymous> (jfeLib.fa2484b7040929bdf0ac.min.js:2:125561), <anonymous>:25:56)
at A.i.<anonymous> (jsApi.8c8dd06a862b12e2d92e.min.js:1:5034)
at A.i.r (jfeLib.fa2484b7040929bdf0ac.min.js:2:29148)
at A.i.s (jfeLib.fa2484b7040929bdf0ac.min.js:2:29191)
at s (jfeLib.fa2484b7040929bdf0ac.min.js:2:28405)
at A.i._trigger (jfeLib.fa2484b7040929bdf0ac.min.js:2:30008)
at A.i.r [as _trigger] (jfe.e7b329a67c066949aeb4.min.js:2:368934)
at A.i._finishRenderAndTriggerReady (jfe.e7b329a67c066949aeb4.min.js:2:283139)
at A.i.r [as _finishRenderAndTriggerReady] (jfe.e7b329a67c066949aeb4.min.js:2:368934)
at A.i.__postRender (jfe.e7b329a67c066949aeb4.min.js:2:263578) ƒ () { var matrixQuestionID = "QR~QID22~";
var partyNames = [
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G"
];
var ratings = [];
For reference, this is the code I've written in the matrix table JavaScript: Qualtrics.SurveyEngine.addOnUnload(function()
Qualtrics.SurveyEngine.addOnUnload(function()
{
var matrixQuestionID = "QR~QID22~";
var partyNames = [
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G"
];
var ratings = [];
// Capture Ratings
for (var i = 0; i < partyNames.length; i++) {
var rating = parseInt($(matrixQuestionID+(i+1)).val());
ratings.push({ party: partyNames[i], rating: rating });
}
// Sort ratings
ratings.sort(function(a, b) {
return b.rating - a.rating;
});
// Store ordered parties
for (var j = 0; j < ratings.length; j++) {
Qualtrics.SurveyEngine.setEmbeddedData("SortedParty_" + (j + 1), ratings[j].party);
}
});
I'm certain that it's due to the naming of the question, but I can't figure out how to fix it (I have tried QID22, QR~QID22~ (to match the HTML name), and the name that I gave it in Qualtrics, partyRank). Any help would be great!
Share Improve this question asked Mar 28 at 12:27 WillWill 31 bronze badge 1 |1 Answer
Reset to default 0You're encountering this error: SE API Error: TypeError: Cannot read properties of null (reading 'val')
The issue comes from this line in your code: var rating = parseInt($(matrixQuestionID+(i+1)).val());
This means you're trying to access a DOM element that doesn't exist — $(...) returns null, and .val() on null throws the error.
Why This Is Happening You're constructing an element ID like: "QR~QID22~" + (i+1)
But Qualtrics matrix questions don’t have individual DOM elements with IDs like that for each cell. That's why your $() call fails.
Correct Approach Instead of trying to access matrix cells using DOM IDs, use Qualtrics' built-in JavaScript API methods like:
getChoiceAnswerValue(rowID) getSelectedChoices() Here’s how you can rewrite your code using getChoiceAnswerValue, which works well for Matrix (Single Answer) questions:
Working JavaScript Example Put this code inside the matrix question's JavaScript (use Add JavaScript):
Qualtrics.SurveyEngine.addOnload(function() { var q = this;
var partyNames = [
"Party A", "Party B", "Party C", "Party D", "Party E", "Party F", "Party G"
];
var ratings = [];
// Row IDs start at 1 in Qualtrics
for (var i = 1; i <= partyNames.length; i++) {
var rating = q.getChoiceAnswerValue(i); // gets the selected column value for row i
ratings.push({ party: partyNames[i - 1], rating: parseInt(rating) });
}
// Sort by rating descending
ratings.sort(function(a, b) {
return b.rating - a.rating;
});
// Store sorted parties in embedded data
for (var j = 0; j < ratings.length; j++) {
Qualtrics.SurveyEngine.setEmbeddedData("SortedParty_" + (j + 1), ratings[j].party);
}
});
Additional Tips Use addOnload instead of addOnUnload. onUnload can be unreliable because the page might be navigating away when the script runs. Avoid hardcoding QID if possible. You can use this.questionId to reference the current question dynamically. If you're using column labels as numerical values (e.g., 1–7), make sure your rating scale in the matrix question is set up accordingly.
$(matrixQuestionID+(i+1))
returned null. What is$
, is that something Qualtrics specific? – C3roe Commented Mar 28 at 12:34