Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JavaScript Allowable Element IDs?

Status
Not open for further replies.

cerebalbore

Technical User
Mar 17, 2008
24
GB
I'm a bit of a novice when it comes to JavaScript and I'm slowly getting there using bits and pieces in my project where needed, but this morning something utterly baffling occured. Let me explain...

I had two ASP HiddenFields, one with an ID of WO and the other STEP, both populated on page load (code behind) like this:

<asp:HiddenField ID="WO" runat="server"/>
<asp:HiddenField ID="STEP" runat="server"/>

And after the HTML side of things, I had a simple JavaScript function that got the values of these fields using these statements:

var foo = document.getElementById('<%= WO.ClientID %>').value;
var bar = document.getElementById('<%= STEP.ClientID %>').value;

However I was getting a compile error against the second statement(BC30201 - Expression Expected), which made no sense to me whatsoever. I changed the STEP one to ('ContentPlaceholder3_STEP'), which worked so I knew it wasn't that the field wasn't there. Google wasn't being much help as nobody seemed to have the same problem.

Did some quick tests adding another hidden field and a quick function to grab it's value, and that worked too. The last thing I tried was to change the ID of the hidden field causing the error, and it worked. That changed the ID from STEP to STEP1.

So, has anyone else experienced this behaviour? Why would STEP as an ID be 'illegal'?? Am I wrong in assuming it's the ID causing the issue??

I'm such a noob
 
First thing to note: JavaScript does not issue compile errors. I would guess the error is coming from the ASP side of things.


To really test whether it's ASP choking on the STEP word, create a dry HTML page with no ASP and only a simple js function and run it.

Code:
<script type="text/JavaScript">
function testID()
{
   var myObj = document.getElementById('STEP');
   alert(myObj.value);
   return false;
}



<input type="hidden" id="STEP" value="this input's ID is STEP">

<a href="#" onclick="return testID();">Click Me</a>



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
You can use the browser's error console to check if JS is throwing any errors.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hello Phil,

I tried your code in both an HTML and an ASPX page, and unsurprisingly both worked, so I guess it is something else that caused the error in my project.

I'll have a fiddle about and see if I can replicate the error in a new little project.

Thanks

Kat

I'm such a noob
 
Glad I could help.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top