First Name:

Odd behavior in javascript

I have a table like so

<table>
  <tr id="trRow1" runat="server" style="display: none">
    <td>First Name:</td>
    <td><asp:Label id="lblFirstName" runat="server"></asp:Label></td>
  </tr>
  <tr>
    <td>Last Name:</td>
    <td><asp:Label id="lblLastName" runat="server"></asp:Label></td>
  </tr>
</table>

      

As you can see, initially the first line is not displayed. When the user clicks on a certain radio button on the page, an asynchronous callback occurs, at which time I set the trRow1 style to "inline". Nothing special; Nothing new. It works great.

Or at least until I try the following in a javascript function.

function Test() {       
  var obj = trRow1.getElementsByTagName("select");
  alert(obj.length);
}

      

The moment I call Test (), the error message "Microsoft JScript Runtime Error:" trRow1 "undefined."

I guess this is because I was messing around with customizing the Display style with AJAX and for some reason the DOM can't find trRow1 even after I set the display to "inline".

Can someone throw me a bone on this? I am stuck.

+1


a source to share


3 answers


The trDegree object is undefined, according to your naming conventions it looks like trDegree is a table row element, I think you are trying to do something like this:

function WTF() {       
  var trDegree = document.getElementById('trDegree'); // Locate the element
  var obj = trDegree.getElementsByTagName("select");
  alert(obj.length);
}

      



Additional link:

+3


a source


I don't see any variable for trDegree in your example. You will need to load trDegree before calling getElementsByTagName.

For example:

function WTF() {
  var trDegree = document.getElementById('trDegree'); 
  var obj = trDegree.getElementsByTagName("select");
  alert(obj.length);
}

      



or you can just load tags from the document level. I'm not sure if this is the effect you want.

function WTF() {
  var obj = document.getElementsByTagName("select");
  alert(obj.length);
}

      

+1


a source


The solution is in the first answer. U must get the element before using it by adding this line:

var trRow1 = document.getElementById('trRow1');

      

0


a source







All Articles