Survey Forums

HomeHomeSurvey Project ...Survey Project ...Using Survey & ...Using Survey & ...get current date function?get current date function?
Previous
 
Next
New Post
9/3/2012 2:29 PM
 

Ok, apologies for the late reply, but I am actually working on a questionnaire for a client and Im a bit limited by the deadline.

Anyways, the solution is simple:

In order for the Javascript code to work, it requires to have the script after/below the field of interest. By creating a new answer type, this does not happen. What you have to do is create a new static free text below the question. This cannot work, though, on its own since the names and ids of the fields change dynamically.

The solution to the above problem is quite simple: Regular expressions. By using regular expressions you seek out the field based on its constant values:

ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as23412$_ai1104_as23412$ctl01

Whenever I looked at the names of the fields I noticed that the highlighted part above changed for the same question every time I ran the previewer or refreshed the page, and in the Preview mode it included a: "$SurveyPreview" element in the name. So I decided to find a way to look for a pattern in the name attribute of the field, instead of looking for a specific name.

Through trial and error I came up with this:

(ctl00\$ctl00\$ContentPlaceHolder1\$ContentPlaceHolder1\$Question19\$_as[0-9]{5}\$_ai1104_as[0-9]{5}\$ctl01)|(ctl00\$ctl00\$ContentPlaceHolder1\$ContentPlaceHolder1\$SurveyPreview\$Question19\$_as[0-9]{5}\$_ai1104_as[0-9]{5}\$ctl01)

Each field is connected through its name to the question as well as its ID in the database, so this way you will never find two fields with duplicate names and also you make sure that the code works in both Form Builder and Survey Preview mode.

Below is the code I used to set the value of a specific field:

<script type="text/javascript">
var d = new Date();
var curr_hour = d.getHours();
var curr_min = d.getMinutes();
curr_min = curr_min + "";
if(curr_min.length == 1) {
                                                curr_min = "0" + curr_min;
                                            }
                      
var regex = /(ctl00\$ctl00\$ContentPlaceHolder1\$ContentPlaceHolder1\$Question19\$_as[0-9]{5}\$_ai1104_as[0-9]{5}\$ctl01)|(ctl00\$ctl00\$ContentPlaceHolder1\$ContentPlaceHolder1\$SurveyPreview\$Question19\$_as[0-9]{5}\$_ai1104_as[0-9]{5}\$ctl01)/gi; //the regular expression for finding a specific text field

var x = document.getElementsByTagName("input"); //seeks the input tag elements of the page
var i=0;

for (i=0; i<x.length; i++){ //runs a loop in order to return all the input elements in turn.


var str = x[i].name; // it takes the name of each input tag visited

var result = regex.exec(str); //runs the regular expression through it.

if(result==null){continue;}else{break;} // On first match break out of the loop, else continue with the loop

 }
var a = result[0]; //take the first attribute of the match
var objName = document.getElementsByName(a).item(0); //find the name of the first attribute of the match

objName.value =  curr_hour + " : " + curr_min; // set the value of the match to whatever you want
                                          
</script>

From this point on you can set up the value and/or the attributes of that field by using the objName and adding the appropriate attribute next to it (.value, .type, etc) 

 The regular expression used above is for text fields only. For radio buttons I discovered its better to use the ID of the field since the name remains the same for radio button fields in the same question, but the Id changed per radio input. For example:

 The ID attributes of radio button inputs follow this pattern:

ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_SurveyPreview_Question248__as[0-9]{5}__ai967_as[0-9]{5}_ctl00

In radio buttons you dont need the non-SurveyPreview regex simply because radio options can be chosen by default. This way you manage to save space, cause the text areas where you put the code are limited to a specific number.

Below is an example of finding the field of a specific radio button option:

var regexht4c = /ctl00_ctl00_ContentPlaceHolder1_ContentPlaceHolder1_SurveyPreview_Question248__as[0-9]{5}__ai967_as[0-9]{5}_ctl00/g;

var xht4c = document.getElementsByTagName("input");

var i=0;

for (i=0; i<xht4c.length; i++){


var strht4c = xht4c[i].id;

var resultht4c = regexht4c.exec(strht4c);

if(resultht4c==null){continue;}else{break;}

 }
var aht4c = resultht4c[0];
var objNameht4c = document.getElementById(aht4c); 

The idea behind it is the same as with the text fields, only instead of finding names of fields, you find IDs.

Through this same mechanism of finding a field with a dynamically changing name, I managed to overcome several problems like multiple conditions branching, setting the value of the fields dynamically, setting the attributes of the fields (for example: set a basic field into hidden -just found out that), etc...

I hope all these will help anyone who had the same requests as me :)

 
New Post
9/3/2012 3:09 PM
 
Hello,

Thank you for sharing the extensive explanation and solution. I'm sure others will benefit from your research as well. We'll try and test the final setup. Very nice to see you're working out very creative solutions to complicated feature requests.

We hope you keep sharing your insights.



 
New Post
9/5/2012 12:00 PM
 

Thank you for the kind words. Before this I wasnt fairly familiar to Javascript, but now I believe I learned much through this.

One more problem (or rather bug in my code)  I encountered is the fact that in case you revisit the page containing a field with that code, it puts the current time. So if you done the survey 10 minutes before and saved it and go to resume it and go back to the page with that field, it will not show the correct time.

 I did this at first:

if(objName.value===""){
objName.value =  curr_hour + " : " + curr_min;
}else{break;}

But then it never showed the time.

 Then I did this:

 if(objName.value===""){
objName.value =  curr_hour + " : " + curr_min;
}else{objName.value=objName.value;}

Which resulted in the original problem again (displaying the current time and not the time originally in the field.)

 Any ideas?

 

EDIT: Never mind, I simply put an If where if the field is not null then leave the value as is. Works only in Preview and Take Survey mode, though.

 
Previous
 
Next
HomeHomeSurvey Project ...Survey Project ...Using Survey & ...Using Survey & ...get current date function?get current date function?