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?