I’m seeing something very strange, wondering if anyone else has come
across this. Here’s a dumbed-down example to illustrate what I’m seeing.
On an html page I have an asp repeater that’s populated by code behind.
On the html page I put a javascript function like the following:
function test()
{
var tempVar = ‘NavigatorRepeater_ctl02_NavigatorContainer’;
// var tempVar = ‘NavigatorRepeater_‘ + ‘ct102’ + ‘
alert(document.getElementById(tempVar).tagName);
}
This works great as is, but if I comment out the first line above and
uncomment the second line, I get a runtime error in the getElementById
saying ‘An object is required’. What’s really strange is it seems to only
apply to the controls in a repeater). For example, I rewrote my
function to use an asp:label that is not in a repeater, as follows:
function test()
{
var tempVar = ‘Li’ + ‘nk’ + ‘Back’;
alert(document.getElementById(tempVar).tagName);
}
This works fine.
The relevant section of my html is the following:
<asp:Repeater ID=”NavigatorRepeater” runat=”server”
OnItemDataBound=”NavigatorRepeaterBound”>
<ItemTemplate>
<div runat=”server” id=”NavigatorContainer”
style=”MARGIN-TOP: 10px; WIDTH: 100%; CURSOR: pointer; HEIGHT: 20px” >
<asp:Image ID=”Image1″ runat=”server” OnClick=”test()” />
 <asp:Label runat=”server” ID=”Name”/></b>
</div>
<asp:Literal runat=”server” ID=”NavigatorContent” />
</ItemTemplate>
</asp:Repeater>
What I’m trying to do in my real app is have the client-side javascript
access the innerText of each <div> in the repeater. I thought I could
hardcode the id parts ‘NavigatorRepeater_‘ and ‘
and then dynamically obtain the client id that the server puts in the
middle of those two parts, then use that full string to get the innerText.
But I can’t seem to use concatenated strings in the getElementById( ),
at least when that concatenated string represents an id that is in a
repeater. Any ideas?
Thanks, and sorry if this is overly basic, I’m still very new to javascript.