Welcome, Guest
Username: Password: Remember me

TOPIC: onFocus or onBlur?

onFocus or onBlur? 3 years 7 months ago #58054

  • VirGnarus
  • VirGnarus's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Attempting to validate input on a short text question by comparing it to a question from a previous group (different page), with both having to be identical. I have hardly any js or jquery knowledge, so I'm not sure. What's the best way to approach this?

For the trigger, I'm thinking of using either onBlur for the current question that's being validated, or onFocus for the next question which is on the same page, but I don't know how to implement either in this case.

A preemptive thanks.
The administrator has disabled public write access.

Re: onFocus or onBlur? 3 years 7 months ago #58069

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
You can use the jQuery .change() function.

To compare two text answers from different pages (Q1 and Q2):

1) Set up your survey to use JavaScript.

2) Add something like the following to the source of Q2. Replace the following with your IDS:
- SSSSS = Survey ID (line 6)
- GG = Group 1 ID (line 6)
- 11 = Q1 ID (line 6)
- 22 = Q2 ID (line 9)

The script uses {INSERTANS} to retrieve the answer of Q1 and puts a listener on the Q2 input to compare the two whenever there is a change to Q2.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		// Get the Q1 answer
		var q1Ans = '{INSERTANS:SSSSSSXGGX11}';
 
		// A listener on the Q2 input 
		$('#question22 input.text').change(function () { 
 
			// Compare the answers
			if ($(this).val() != q1Ans) {
				alert ('The answers do not match!!!');
			}
			else{
				alert ('The answers do match!!!');
			}
		});
 
	});
 
</script>
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 7 months ago by tpartner.
The administrator has disabled public write access.

Re: onFocus or onBlur? 3 years 7 months ago #58070

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
The WYSIWYG editor was messing up the script so please use the updated script and instructions above.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
The administrator has disabled public write access.
The following user(s) said Thank You: VirGnarus

Re: onFocus or onBlur? 3 years 7 months ago #58099

  • VirGnarus
  • VirGnarus's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Thanks a lot. I was thinking it needed to be specified when to check, either by losing focus on current form or gaining focus on the next one. Other options I tried alerted immediately on question load.

I'm also seeing a lot of varying differences between how answers can be referenced (#questionID,SGQ,INSERTANS,etc.). I'm not exactly sure which ones are appropriate at which times (manual is rather vague on it). Is there any resource that clarifies on this? Any glossary or resource that lists functions and syntax that is exclusive to Limesurvey?

Again, thanks for everything.
The administrator has disabled public write access.

Re: onFocus or onBlur? 3 years 7 months ago #58100

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
Unfortunately there is no documentation on the specific selectors for different question/answer elements - they are by necessity quite varied. (maybe a project for the future)

All I can suggest is using a tool like Firebug for Firefox which allows you to explore the DOM elements.
Cheers,
Tony Partner

Solutions, code and workarounds presented in these forums are given without any warranty, implied or otherwise.

LimeSurvey is open-source and run entirely by volunteers so please consider donating to support the project.
Last Edit: 3 years 7 months ago by tpartner.
The administrator has disabled public write access.

Re: onFocus or onBlur? 3 years 7 months ago #58107

  • VirGnarus
  • VirGnarus's Avatar
  • OFFLINE
  • Fresh Lemon
  • Posts: 3
  • Karma: 0
Yeah, I use Firebug very frequently, I guess this will be just one further use for it.

On another note, I'm having difficulties implementing this in current surveys. On test survey it works perfectly, and regardless of browser used. However, on all other surveys I've added it too, not one of them works. No syntax errors or anything brought up. Here's the differences between the test survey I used that works and the surveys I added the script into:

Test:

- 1 Group
- Separate single page per question
- Question 2 (page two) looks at question 1 (page 1).

Others:

- Multiple Groups
- Each group has a single page.
- First question for each group (and page) are the ones being validated, so:
- Question 1 for group 2 (page 2) looks at Question 1 for group 1 (page 1)


I notice in Firebug that it grabs the var from the first question just fine. However it doesn't do anything with it. I suspect it may be a problem with the selector for the second question (current #answerQQ). I've altered it a couple ways (#answerSSSSSXGGGXQQ, #questionSSSSSXGGGXQQ, #questionQQ) but no dice. No errors that I see reported, and no prompt once focus is lost from the question (or attempting to submit). Any ideas?
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.235 seconds
Donation Image