Welcome, Guest
Username: Password: Remember me

TOPIC: Pass javascript into answer

Pass javascript into answer 3 years 5 months ago #61114

Hi,

We need to track the User Agent String for our survey responders. I figured the best way to do this would be to create a question, hide it, and then use JavaScript to push the user agent string into the response to be saved in our database.

So, I am to get the user agent string fine (and I have tested this with an alert) and hide the question, but I can't populate the textbox with the user agent string, I keep getting a null warning in javascript. Any suggestions? Here's the code I'm inserting into the question:

<script type="text/javascript" charset="utf-8">


$(document).ready(function() {

$('#question1').hide();
});


txt = "Browser CodeName: " + navigator.appCodeName + "@@";
txt+= "Browser Name: " + navigator.appName + "@@";
txt+= "Browser Version: " + navigator.appVersion + "@@";
txt+= "Cookies Enabled: " + navigator.cookieEnabled + "@@";
txt+= "Platform: " + navigator.platform + "@@";
txt+= "User-agent header: " + navigator.userAgent + "@@";


var inputObj = document.getElementById("answer11118X36X514");
if( inputObj ) {
inputObj.value = inputObj ;
}

</script>


Thanks!!!
The administrator has disabled public write access.

Re: Pass javascript into answer 3 years 5 months ago #61145

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
Assuming that the ID of your hidden text question id 514:
<script type="text/javascript" charset="utf-8">
 
	$(document).ready(function() {
 
		$('#question514').hide();
 
 
		var txt = "Browser CodeName: " + navigator.appCodeName + "@@";
		txt+= "Browser Name: " + navigator.appName + "@@";
		txt+= "Browser Version: " + navigator.appVersion + "@@";
		txt+= "Cookies Enabled: " + navigator.cookieEnabled + "@@";
		txt+= "Platform: " + navigator.platform + "@@";
		txt+= "User-agent header: " + navigator.userAgent + "@@";
 
		var inputObj = document.getElementById("answer11118X36X514");
		if( inputObj ) {
			inputObj.value = txt ;
		}
	});
 
</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.

Re: Pass javascript into answer 3 years 5 months ago #61179

Thanks, I had inadvertently messed up the javascript when I was pasting it into this forum because I was removing other code that was not applicable.

I realized my problem was caused by the fact that the javascript was firing before the textarea was created so that's why it couldn't find it and was null. So, I moved the javascript to the Help text section instead of the question text section and it worked great. Thanks for your help!
The administrator has disabled public write access.

Re: Pass javascript into answer 3 years 5 months ago #61195

  • tpartner
  • tpartner's Avatar
  • NOW ONLINE
  • LimeSurvey Team
  • Posts: 4281
  • Thank you received: 807
  • Karma: 370
I realized my problem was caused by the fact that the javascript was firing before the textarea was created so that's why it couldn't find it and was null.
Yes, putting it all in the $(document).ready function prevents it from being executed until after the entire page is loaded so all DOM elements are accessible.
So, I moved the javascript to the Help text section instead of the question text section and it worked great.
JavaScript can be placed in either of these or in the group description text section if it appears on the 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.
Moderators: ITEd
Time to create page: 0.098 seconds
Donation Image