Welcome, Guest
Username: Password: Remember me

TOPIC: How to insert a JS code input into the answet itself?

How to insert a JS code input into the answet itself? 2 years 5 months ago #78047

Hi,

Using LimeService.

I have the following code in one of my questions:
<script type="text/javascript">
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName  = navigator.appName;
var fullVersion  = ''+parseFloat(navigator.appVersion); 
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;
 
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
 browserName = "Opera";
 fullVersion = nAgt.substring(verOffset+6);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome" 
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = nAgt.substring(verOffset+7);
 if ((verOffset=nAgt.indexOf("Version"))!=-1) 
   fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox" 
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = nAgt.substring(verOffset+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < 
          (verOffset=nAgt.lastIndexOf('/')) ) 
{
 browserName = nAgt.substring(nameOffset,verOffset);
 fullVersion = nAgt.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
   fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
   fullVersion=fullVersion.substring(0,ix);
 
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVersion = parseInt(navigator.appVersion,10);
}
 
document.write(''
 +'Browser name  = '+browserName+'<br>'
 +'Full version  = '+fullVersion+'<br>'
 +'Major version = '+majorVersion+'<br>'
 +'navigator.appName = '+navigator.appName+'<br>'
 +'navigator.userAgent = '+navigator.userAgent+'<br>'
)
</script>

The code returns:



How do I get it to write the results in the answer itself (short free text) and not as the question?

Here's a sample survey with said question.

Thanks,
David
The administrator has disabled public write access.

Re: How to insert a JS code input into the answet itself? 2 years 5 months ago #78068

  • Mazi
  • Mazi's Avatar
  • OFFLINE
  • LimeSurvey Team
  • Posts: 5325
  • Thank you received: 296
  • Karma: 249
This can also be done by using Javascript. Each text field has a unique ID which makes it eas to fill it using JS.
Please use the forum search, there should be several examples (please post the link/solution afterwards).

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: How to insert a JS code input into the answet itself? 2 years 5 months ago #78073

  • floccs
  • floccs's Avatar
  • OFFLINE
  • Senior Lime
  • Posts: 47
  • Thank you received: 5
  • Karma: 2
At the end of your script instead of document.write( ... ) use:

$("#answer{SGQ}").val(''
+'Browser name = '+browserName+'<br>'
+'Full version = '+fullVersion+'<br>'
+'Major version = '+majorVersion+'<br>'
+'navigator.appName = '+navigator.appName+'<br>'
+'navigator.userAgent = '+navigator.userAgent+'<br>');
The administrator has disabled public write access.

Re: How to insert a JS code input into the answet itself? 2 years 5 months ago #78117

Thank you both!

Replacing the last piece of code with this code did the trick:
$(document).ready(function() {
 	$('#answer15477X82X881').val(''
		+'Browser name = '+browserName+'\n'
		+'Full version = '+fullVersion+'\n'
		+'Major version = '+majorVersion+'\n'
		+'navigator.appName = '+navigator.appName+'\n'
		+'navigator.userAgent = '+navigator.userAgent
	); 
});
The administrator has disabled public write access.
Moderators: ITEd
Time to create page: 0.220 seconds
Donation Image