/    Sign up×
Community /Pin to ProfileBookmark

Concatenated strings with getElementByID

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’ + ‘_NavigatorContainer’;
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()” />
&nbsp;&nbsp<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 ‘_NavigatorContainer’,
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.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 12.2009 — 
var tempVar = 'NavigatorRepeater_ctl02_NavigatorContainer';

// var tempVar = 'NavigatorRepeater_' + 'ct102' + '_NavigatorContainer';
[/QUOTE]
To my old eyes it appears that one has a

'l' (lower case 'L') and the other has a

'1' (number one).

Could you check that?
Copy linkTweet thisAlerts:
@doosterbaanauthorMar 13.2009 — Unbelievable. Apparently your "old eyes" are better than my young

eyes. Or maybe that's just experience. You're right, I corrected that

and it is working much better now. Thanks so much, and sorry for

bothering you with a typo. It is comforting to know that this javascript

is actually behaving the way I would intuitively expect it to.

Thanks again.
×

Success!

Help @doosterbaan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...