Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1
  • 2

TOPIC: insert javascript

insert javascript 2 years 4 months ago #80542

  • mattias
  • mattias's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 77
  • Karma: 1
I have a function where I try to evaluate two numerical fields where the value 1 must be <= value 2

example

pantalla_2012-05-17.jpg


my script
<script type="text/javascript" charset="utf-8">
function validation()
{
   if (({INSERTANS:27832X11242X124482}<={INSERTANS:27832X11242X124483}))
   {
 
     document.limesurvey.move.value = 'moveprev';
     document.limesurvey.submit();
   }
   else
   {
	 alert("value 1 must be <= to value 2");
     document.limesurvey.move.value = 'movelast';
     document.limesurvey.submit();
   }
}
 
setTimeout("validation()",250);
 
</script>

regards
Last Edit: 2 years 4 months ago by mattias.
The administrator has disabled public write access.

Re: insert javascript 2 years 4 months ago #80543

  • TMSWhite
  • TMSWhite's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 759
  • Thank you received: 82
  • Karma: 36
If you use 1.92+, you don't need the JavaScript. Instead, use the "minimum value" advanced question option.

Screen-shots and working examples can be found here.
The administrator has disabled public write access.

Re: insert javascript 2 years 4 months ago #80544

  • mattias
  • mattias's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 77
  • Karma: 1
At the moment is a bit difficult to update to version 1.92, since I have lots of data.

regards
The administrator has disabled public write access.

Re: insert javascript 2 years 4 months ago #80570

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4195
  • Thank you received: 789
  • Karma: 359
Are both questions on the same page?
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.

Re: insert javascript 2 years 4 months ago #80615

  • DenisChenu
  • DenisChenu's Avatar
  • OFFLINE
  • Moderator Lime
  • Posts: 6338
  • Thank you received: 819
  • Karma: 243
Hello,

No need a time out function.
Try:
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("input.submit").click(function(){
   if ($("#question124482 input.text").val()<=$("#question124483 input.text").val()))
   {
       return true
   }
   else
   {
	 alert("value 1 must be <= to value 2");
     return false;
   }
});
});
</script>
( maube some error in code, but basis is here).
replace the number by the question real number.
Denis
Last Edit: 2 years 4 months ago by DenisChenu.
The administrator has disabled public write access.

Re: insert javascript 2 years 4 months ago #80702

  • mattias
  • mattias's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 77
  • Karma: 1
Denis and tpartner thanks, but I throw an error code, the questions are on the same page, also change the codes of the questions.

regards
The administrator has disabled public write access.

Re: insert javascript 2 years 4 months ago #80729

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5325
  • Thank you received: 296
  • Karma: 249
Please activate a test survey and post the link here so we can have a look at the HTML source.

Best regards/Beste Grüße,
Dr. Marcel Minke
(Limesurvey Head of Support)
Need Help? We offer professional Limesurvey support
Contact: marcel.minke(at)limesurvey.org'"
The administrator has disabled public write access.

Re: insert javascript 2 years 4 months ago #80736

  • mattias
  • mattias's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 77
  • Karma: 1
this is the file I'm editing.
Attachments:
The administrator has disabled public write access.

Re: insert javascript 2 years 4 months ago #80762

  • tpartner
  • tpartner's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 4195
  • Thank you received: 789
  • Karma: 359
I would validate the inputs dynamically instead of waiting for the submit click.

Assuming there are only two numeric questions on the page, you can use this without messing with IDs.

The script puts listeners on the inputs and if a second value is found to be higher than the first, the respondent is alerted and the last value entered is removed.
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function(){
 
		var text = 'The first value must not be higher than the second. Please re-enter your value.';
 
		$('.numeric input.text').change(function(){
			if($('.numeric:eq(0) input.text').val() && $('.numeric:eq(1) input.text').val() && $('.numeric:eq(0) input.text').val() > $('.numeric:eq(1) input.text').val()) {
				alert (text);
				$(this).val('');
			}
		});
	});
 
</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.
The administrator has disabled public write access.
The following user(s) said Thank You: mattias

Re: insert javascript 2 years 4 months ago #80789

  • mattias
  • mattias's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 77
  • Karma: 1
tparnert worked thank you very much for your excellent script.

regards
The administrator has disabled public write access.
  • Page:
  • 1
  • 2
Moderators: ITEd
Time to create page: 0.193 seconds
Donation Image