Survey Forums

HomeHomeSurvey Project ...Survey Project ...Using Survey & ...Using Survey & ...get current date function?get current date function?
Previous
 
Next
New Post
8/6/2012 12:12 PM
 

Oh, thats a shame. Well, I guess I will have to do it manually for the time being by finding the id by viewing the page's source code, and meanwhile try to find a way through this. Thanks for the info.

Also that link points to no discussion. Says its not there.

 EDIT: Just an idea, you think the piping feature could work here?

 
New Post
8/7/2012 5:20 PM
 

Ok there is something weird happening:

 I decided to create a new answer type and place my javascript code there and select that type for my answer type. I added the custom name that the SP generates, directly into the javascript code. It didnt work.

I thought it could be some of the symbols or something that break down the script, so I tried it outside on an empty page. It worked like a charm.

 

Here is my code:

 -------------------------------

<form name="aspnetForm1">
<input type="text" id="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" size=11>
</form>

<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;
   }


document.getElementById("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as"+/\d{5}/+"$_ai1104_as"+/\d{5}/+"$ctl01").value=curr_hour + " : " + curr_min;

//-->
</script>

--------------------------------

So here is my question: What could it be that blocks the script from executing? It can't be the <script> tags cause I tried it without them. Could it be the fact that I didnt add a script function name?

 

EDIT: So the naming is indeed fully automatic on certain parts:

 ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as[random]$_ai1104_as[random]$ctl01

 My guess is there is an algorithm for this, any hints on where to look for it?

 

EDIT #2:

 

I think I am close to applying the pattern:

 

<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 refldname="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as"+/^\d{5}$/+"$_ai1104_as"+/^\d{5}$/+"$ctl01";

document.getElementById("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as+/^\d{5}$/+$_ai1104_as+/^\d{5}$/+$ctl01").value=curr_hour + " : " + curr_min;
document.aspnetForm.refldname.value=curr_hour + " : " + curr_min;
//-->
</script>

 

But Im not sure how it will know which field is it. Is it the $_ai[number] part?

 

Edited: Corrected the code with the one in dark red fonts. If There were no random numbers and it was:

document.getElementById("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01").value=curr_hour + " : " + curr_min;

 ...it works. So It seems my error is here:

"ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as+/^\d{5}$/+$_ai1104_as+/^\d{5}$/+$ctl01"

I have tried it as:

 "ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as"+/^\d{5}$/+"$_ai1104_as"+/^\d{5}$/+"$ctl01"

 and

ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as+/^\d{5}$/+$_ai1104_as+/^\d{5}$/+$ctl01

but it didn't work. Any ideas?

 

EDIT#2:

 

It does work as:

"ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as"+"12345"+"$_ai1104_as"+"12345"+"$ctl01"

Im so close...

 

 

EDIT#3:

 

I found the regular expression that will return me a specific alphanumeric sequence which is unique to each field:

 \w{2}[1104]{4}

 

\w{2} : starts with 2 letters

[1104] : Refers to the unique number in each field id which remains constant

 {4} : Refers to 4 digits, limiting the results to just one

In essence it looks for a sequence which starts with 3 letters, contains the number 1104 and has 4 digits.

 

Still I have to figure out how to apply this in the code, so that it finds the field which contains the match of the above Regex and to call it in: document.getElementById

 

Any ideas?

 

 
New Post
8/7/2012 11:15 PM
 
Why dont you try using: document.getelementsbyname and apply the regex.

You're almost there!

 
New Post
8/8/2012 10:28 AM
 

This is the workable version of the code without the regex and using getElementById. For some reason getElementsByName doesnt work for me. Not sure what it is, Im looking into it. In the meanwhile feel free to give feedback :) .

 

<!DOCTYPE html>

<html>
<head></head>
<body>
<form name="test">
<input type="text" id="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" size=11 />
</form>

<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 a=document.getElementById("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01");

a.value=curr_hour + " : " + curr_min;

document.write(curr_hour + " : " + curr_min);

//-->
</script>

</body>

</html>

 


EDIT#1: Can anyone explain to me why this code works?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!DOCTYPE html>
<head></head>
<body>
<form name="test">
<input type="text" id="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" size=11 />
</form>

<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 objName=ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01;


objName.value=curr_hour + " : " + curr_min;

document.write(curr_hour + " : " + curr_min);

//-->
</script>

</body>

</html>

while this doesnt?

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!DOCTYPE html>
<head></head>
<body>
<form name="test">
<input type="text" id="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" size=11 />
</form>

<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 objName=getElementsByName("ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01");


objName.value=curr_hour + " : " + curr_min;

document.write(curr_hour + " : " + curr_min);

//-->
</script>

</body>

</html>


EDIT#2:

 

Ok I decided to use a slightly different approach than what you proposed. Instead of getElementsByName() method I decided to go for getElementsByTagName() method. Here is the code:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!DOCTYPE html>
<head></head>
<body>
<form name="test">
<input type="text" id="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" size=11 />
</form>
<script type="text/javascript">
<!--

/*beginning of getting time algorithm*/
var d = new Date();

var curr_hour = d.getHours();
var curr_min = d.getMinutes();
if (curr_min.length == 1)
   {
   curr_min = "0" + curr_min;
   }
  curr_min = curr_min + "";

/*end of getting time algorithm*/
   
var regex1=/\w{2}[1104]{4}/; //looks for a the number 1104 in the string
var regex2=/[0-9]{5}/; //looks for any 5 digit number

var x=getElementsByTagName("input"); //gets the input tag elements
var str = x[0].name; // The name attribute of the first occurance
var result1 = regex1.exec(str); //searches for number 1104 in the name attribute of input tags
var result2 = regex2.exec(str); //searches for a 5 digit number in the name attribute of input tags

var objName="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as"+result2[0]+"$_"+result1[0]+"_as"+result2[0]+"$ctl01"; /* reconstructing the name*/


objName.value=curr_hour + " : " + curr_min;
document.write(curr_hour + " : " + curr_min); //just to see if the code breaks somewhere

//-->
</script>
</body>

</html>

 

Still for some reason it doesnt work. I might be blind to the error. Any help appreciated.

 


EDIT#3:

 

I decided tp change the regex to something different combining regex1 and regex2:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!DOCTYPE html>
<head></head>
<body>
<form name="test">
<input type="text" id="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" size=11 />
</form>
<script type="text/javascript">
<!--

/*beginning of getting time algorithm*/
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;
   }


/*end of getting time algorithm*/
   
var regex=/ctl00\$ctl00\$ContentPlaceHolder1\$ContentPlaceHolder1\$Question19\$_as[0-9]{5}\$\wai1104\was[0-9]{5}\$ctl01/; /*Is the pattern*/
var x=document.getElementsByTagName("input"); //gets the input tag elements
var str = x[0].name; // The name attribute of the first occurance
var result = regex.exec(str); //searches in the name attribute of input tags for the pattern sequence


var objName=result[0];



objName.value=curr_hour + " : " + curr_min;
document.write(curr_hour + " : " + curr_min); //just to see if the code breaks somewhere

//-->
</script>
</body>

</html>

 

Still it does not work though :(

 

(Edited with correction on the highlighted)

 
New Post
8/8/2012 3:52 PM
 

I think I got the code to work. Still it wont run when I add this as a new answer type:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<!DOCTYPE html>
<head></head>
<body>
<form name="test">
<input type="text" id="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" name="ctl00$ctl00$ContentPlaceHolder1$ContentPlaceHolder1$Question19$_as12345$_ai1104_as12345$ctl01" size=11 />
</form>
<script type="text/javascript">
<!--

/*beginning of getting time algorithm*/
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;
   }
 

/*end of getting time algorithm*/

var regex=/ctl00\$ctl00\$ContentPlaceHolder1\$ContentPlaceHolder1\$Question19\$_as[0-9]{5}\$\wai1104\was[0-9]{5}\$ctl01/; //defines the pattern

var x = document.getElementsByTagName("input"); //gets the input tag elements

var str = x[0].name; // The name attribute of the first occurance

var result = regex.exec(str); //searches for pattern

document.write("Time:" + " "+curr_hour + " : " + curr_min); //just to see if the code breaks somewhere
// result);

var a=result[0];
// result[0]);
var objName = document.getElementsByName(a).item(0);
objName.value=curr_hour + " : " + curr_min;

//-->
</script>
</body>

</html> 

 

 I viewed the source code of the survey builder with Firebug. I went at the specific field and I couldnt find the javascript code anywhere. Do I have to add something in the default value?

 

Questions:

 

- How is it the built in JavaScripts work? (just in general)

- When I choose to make an answer type with javascript, what is the format I should choose? (i.e. add the <script> tags, do I need to have it as a function, etc...)

- The field "Javascript function name :" is it mandatory?

- What do I need to put in "Error message :" field?

 

As you see Im kinda lost there. Thank you for your help so far :)

 

Edit: Ok so apparently Im supposed to make this javascript into a function, am I correct? I tried it already and for some reason I couldn't make it to work. Any directions? (javascript isnt my forte :P) 

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