admin管理员组文章数量:1291295
I am using a toy example for the purpose of my questions. I would like to ask what should I change in below code so that I can record QID and the starRating in response data.
Thank you.
I have a survey block QID1 in html,
<div class="form-group">
<label for="rating">Rate Our Service:</label>
<div class="stars" id="starRating">
<span data-value="1">★</span>
<span data-value="2">★</span>
<span data-value="3">★</span>
<span data-value="4">★</span>
<span data-value="5">★</span>
</div>
<input type="hidden" id="ratingInput" name="rating" required>
</div>
and the CCS code I pass to Qualtrics -> Look at Feel is here:
.stars span {
font-size: 32px;
color: gray;
cursor: pointer;
transition: color 0.2s;
}
.stars span.selected {
color: gold;
}
This is the JavaScript I put for this survey block:
Qualtrics.SurveyEngine.addOnload(function() {
// Interactive star rating
const stars = document.querySelectorAll('#starRating span');
const ratingInput = document.getElementById('ratingInput');
stars.forEach(star => {
star.addEventListener('click', function () {
const ratingValue = this.getAttribute('data-value');
ratingInput.value = ratingValue;
stars.forEach(s => s.classList.remove('selected'));
for (let i = 0; i < ratingValue; i++) {
stars[i].classList.add('selected');
}
});
});
});
I tried to edit the JavaScript as below, it certainly did not work:
Qualtrics.SurveyEngine.addOnload(function() {
const stars = document.querySelectorAll('#QID1 span');
const ratingInput = document.getElementById('QID1_ratingInput');
const serviceRatingQuestion = Qualtrics.SurveyEngine.getQuestionById("QID1");
stars.forEach(star => {
star.addEventListener('click', function () {
const ratingValue = this.getAttribute('data-value');
ratingInput.value = ratingValue;
stars.forEach(s => s.classList.remove('selected'));
for (let i = 0; i < ratingValue; i++) {
stars[i].classList.add('selected');
}
// Set the selected choice in Qualtrics
serviceRatingQuestion.setChoiceValue(ratingValue, true);
});
});
});
Appreciate if you can point me to some thread that is relevant to the problem I have.
I am using a toy example for the purpose of my questions. I would like to ask what should I change in below code so that I can record QID and the starRating in response data.
Thank you.
I have a survey block QID1 in html,
<div class="form-group">
<label for="rating">Rate Our Service:</label>
<div class="stars" id="starRating">
<span data-value="1">★</span>
<span data-value="2">★</span>
<span data-value="3">★</span>
<span data-value="4">★</span>
<span data-value="5">★</span>
</div>
<input type="hidden" id="ratingInput" name="rating" required>
</div>
and the CCS code I pass to Qualtrics -> Look at Feel is here:
.stars span {
font-size: 32px;
color: gray;
cursor: pointer;
transition: color 0.2s;
}
.stars span.selected {
color: gold;
}
This is the JavaScript I put for this survey block:
Qualtrics.SurveyEngine.addOnload(function() {
// Interactive star rating
const stars = document.querySelectorAll('#starRating span');
const ratingInput = document.getElementById('ratingInput');
stars.forEach(star => {
star.addEventListener('click', function () {
const ratingValue = this.getAttribute('data-value');
ratingInput.value = ratingValue;
stars.forEach(s => s.classList.remove('selected'));
for (let i = 0; i < ratingValue; i++) {
stars[i].classList.add('selected');
}
});
});
});
I tried to edit the JavaScript as below, it certainly did not work:
Qualtrics.SurveyEngine.addOnload(function() {
const stars = document.querySelectorAll('#QID1 span');
const ratingInput = document.getElementById('QID1_ratingInput');
const serviceRatingQuestion = Qualtrics.SurveyEngine.getQuestionById("QID1");
stars.forEach(star => {
star.addEventListener('click', function () {
const ratingValue = this.getAttribute('data-value');
ratingInput.value = ratingValue;
stars.forEach(s => s.classList.remove('selected'));
for (let i = 0; i < ratingValue; i++) {
stars[i].classList.add('selected');
}
// Set the selected choice in Qualtrics
serviceRatingQuestion.setChoiceValue(ratingValue, true);
});
});
});
Appreciate if you can point me to some thread that is relevant to the problem I have.
Share Improve this question asked Feb 13 at 15:04 babyababya 111 bronze badge1 Answer
Reset to default 0I assume you know that Qualtrics has a built-in star rating question so this example is doing it the hard way. I also assume that your html and JS is attached to a multiple choice question.
A few things to note:
- Within addOnload 'this' points to the question object and getQuestionById isn't a Qualtrics JS API function
- Since 'this' points to the question object you don't need to hard code the question id (QID1)
- The hidden ratingInput element in your html serves no purpose
So your JS should look like the following:
Qualtrics.SurveyEngine.addOnload(function() {
const qobj = this;
const stars = document.querySelectorAll('#starRating span');
stars.forEach(star => {
star.addEventListener('click', function () {
const ratingValue = this.getAttribute('data-value');
stars.forEach(s => s.classList.remove('selected'));
for (let i = 0; i < ratingValue; i++) {
stars[i].classList.add('selected');
}
// Set the selected choice in Qualtrics
qobj.setChoiceValue(ratingValue, true);
});
});
});
本文标签: qualtricsRecord response for survey block using htmlJavaScriptStack Overflow
版权声明:本文标题:qualtrics - Record response for survey block using html+JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741527545a2383558.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论