Home All Groups Group Topic Archive Search About

how to get the text in JS of a cell created on server?

Author
16 Jun 2006 3:36 PM
Bob
Hi,

I have already posted a similar problem but it 's still a problem for me, so
...

I define a table in aspx file and Javascript code.
Purpose: when the user clicks on any cell of the table, the text of it must
be rendered in an Alert.

<asp:Table ID="table1" runat="server">
</asp:Table>

<script language="javascript" type="text/javascript">
function tableclick(event)
{
strid=window.event.srcElement.id
alert(strid)                           // this works: i get e.g. 3:2
strid=window.event.srcElement.text}
alert(strid)                          // this doens't work ("undefined")
}

I create row and cells in the code-behind:
Dim r As TableRow
Dim c(50, 20) As TableCell
For i = 0 To 50
r = New TableRow()
For j = 0 To 20
c(i, j) = New TableCell()
c(i, j).ID = j & ":" & i.ToString
r.Cells.Add(c(i, j))
Next
Table1.Rows.Add(r)
Next

c(3, 2).Text = "this is the text i want in Javascript"
....

My problem is that  the Alert gives "undefined".
I can get the 'ID' of each cell, why not the 'text'?
I know there is the property 'ClientID' but it's overruled by the ID i
defined in the code, so that's not the point (i think).

Thanks for help.
Bob

Author
16 Jun 2006 3:47 PM
Marina Levit [MVP]
Depending on the type of element it is, it may not have a 'text' property.
Try 'innerText' or 'innerHTML' depending on what you want.

I recommend you use the following reference for DHTML so you know what you
can do with what objects:
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects.asp?frame=true

Show quoteHide quote
"Bob" <s**@sdvsd.dc> wrote in message
news:%237cjzqVkGHA.976@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> I have already posted a similar problem but it 's still a problem for me,
> so
> ..
>
> I define a table in aspx file and Javascript code.
> Purpose: when the user clicks on any cell of the table, the text of it
> must
> be rendered in an Alert.
>
> <asp:Table ID="table1" runat="server">
> </asp:Table>
>
> <script language="javascript" type="text/javascript">
> function tableclick(event)
> {
> strid=window.event.srcElement.id
> alert(strid)                           // this works: i get e.g. 3:2
> strid=window.event.srcElement.text}
> alert(strid)                          // this doens't work ("undefined")
> }
>
> I create row and cells in the code-behind:
> Dim r As TableRow
> Dim c(50, 20) As TableCell
> For i = 0 To 50
> r = New TableRow()
> For j = 0 To 20
> c(i, j) = New TableCell()
> c(i, j).ID = j & ":" & i.ToString
> r.Cells.Add(c(i, j))
> Next
> Table1.Rows.Add(r)
> Next
>
> c(3, 2).Text = "this is the text i want in Javascript"
> ...
>
> My problem is that  the Alert gives "undefined".
> I can get the 'ID' of each cell, why not the 'text'?
> I know there is the property 'ClientID' but it's overruled by the ID i
> defined in the code, so that's not the point (i think).
>
> Thanks for help.
> Bob
>
>
>
>
>
Author
16 Jun 2006 4:01 PM
Bob
I tried with innerText, innerHtml, text .... alwyas the sale 'undefined'.
In the code-behind, i have no choice: after the dot after c(i,j) i only can
take 'text'.
There must be an equivalence between 'text' in code-behind and a property in
html, no?
..


"Marina Levit [MVP]" <someone@nospam.com> wrote in message
news:umBCkwVkGHA.1324@TK2MSFTNGP04.phx.gbl...
> Depending on the type of element it is, it may not have a 'text' property.
> Try 'innerText' or 'innerHTML' depending on what you want.
>
> I recommend you use the following reference for DHTML so you know what you
> can do with what objects:
>
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects.asp?frame=true
Show quoteHide quote
>
> "Bob" <s**@sdvsd.dc> wrote in message
> news:%237cjzqVkGHA.976@TK2MSFTNGP02.phx.gbl...
> > Hi,
> >
> > I have already posted a similar problem but it 's still a problem for
me,
> > so
> > ..
> >
> > I define a table in aspx file and Javascript code.
> > Purpose: when the user clicks on any cell of the table, the text of it
> > must
> > be rendered in an Alert.
> >
> > <asp:Table ID="table1" runat="server">
> > </asp:Table>
> >
> > <script language="javascript" type="text/javascript">
> > function tableclick(event)
> > {
> > strid=window.event.srcElement.id
> > alert(strid)                           // this works: i get e.g. 3:2
> > strid=window.event.srcElement.text}
> > alert(strid)                          // this doens't work ("undefined")
> > }
> >
> > I create row and cells in the code-behind:
> > Dim r As TableRow
> > Dim c(50, 20) As TableCell
> > For i = 0 To 50
> > r = New TableRow()
> > For j = 0 To 20
> > c(i, j) = New TableCell()
> > c(i, j).ID = j & ":" & i.ToString
> > r.Cells.Add(c(i, j))
> > Next
> > Table1.Rows.Add(r)
> > Next
> >
> > c(3, 2).Text = "this is the text i want in Javascript"
> > ...
> >
> > My problem is that  the Alert gives "undefined".
> > I can get the 'ID' of each cell, why not the 'text'?
> > I know there is the property 'ClientID' but it's overruled by the ID i
> > defined in the code, so that's not the point (i think).
> >
> > Thanks for help.
> > Bob
> >
> >
> >
> >
> >
>
>
Author
16 Jun 2006 4:17 PM
Marina Levit [MVP]
No, there isn't necessarily.

Code behind is just an object with some properties that are intuitive.  In
the end, that in memory object will be turned into a bunch of HTML streamed
down to the browser.  So there is not necessarily a correspondance between
the javascript and .NET properties.

Example, the button class has a Text property. But, when that is streamed
down, it is streamed down as an INPUT element, which has a value HTML
property.

I am guesing that this event is not firing on the element you think it is
firing on. You should find out which element it is, and what type it is, to
see what properties or methods it has available.

Show quoteHide quote
"Bob" <s**@sdvsd.dc> wrote in message
news:%23YWwn4VkGHA.324@TK2MSFTNGP02.phx.gbl...
>I tried with innerText, innerHtml, text .... alwyas the sale 'undefined'.
> In the code-behind, i have no choice: after the dot after c(i,j) i only
> can
> take 'text'.
> There must be an equivalence between 'text' in code-behind and a property
> in
> html, no?
> ..
>
>
> "Marina Levit [MVP]" <someone@nospam.com> wrote in message
> news:umBCkwVkGHA.1324@TK2MSFTNGP04.phx.gbl...
>> Depending on the type of element it is, it may not have a 'text'
>> property.
>> Try 'innerText' or 'innerHTML' depending on what you want.
>>
>> I recommend you use the following reference for DHTML so you know what
>> you
>> can do with what objects:
>>
> http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects.asp?frame=true
>>
>> "Bob" <s**@sdvsd.dc> wrote in message
>> news:%237cjzqVkGHA.976@TK2MSFTNGP02.phx.gbl...
>> > Hi,
>> >
>> > I have already posted a similar problem but it 's still a problem for
> me,
>> > so
>> > ..
>> >
>> > I define a table in aspx file and Javascript code.
>> > Purpose: when the user clicks on any cell of the table, the text of it
>> > must
>> > be rendered in an Alert.
>> >
>> > <asp:Table ID="table1" runat="server">
>> > </asp:Table>
>> >
>> > <script language="javascript" type="text/javascript">
>> > function tableclick(event)
>> > {
>> > strid=window.event.srcElement.id
>> > alert(strid)                           // this works: i get e.g. 3:2
>> > strid=window.event.srcElement.text}
>> > alert(strid)                          // this doens't work
>> > ("undefined")
>> > }
>> >
>> > I create row and cells in the code-behind:
>> > Dim r As TableRow
>> > Dim c(50, 20) As TableCell
>> > For i = 0 To 50
>> > r = New TableRow()
>> > For j = 0 To 20
>> > c(i, j) = New TableCell()
>> > c(i, j).ID = j & ":" & i.ToString
>> > r.Cells.Add(c(i, j))
>> > Next
>> > Table1.Rows.Add(r)
>> > Next
>> >
>> > c(3, 2).Text = "this is the text i want in Javascript"
>> > ...
>> >
>> > My problem is that  the Alert gives "undefined".
>> > I can get the 'ID' of each cell, why not the 'text'?
>> > I know there is the property 'ClientID' but it's overruled by the ID i
>> > defined in the code, so that's not the point (i think).
>> >
>> > Thanks for help.
>> > Bob
>> >
>> >
>> >
>> >
>> >
>>
>>
>
>
Author
16 Jun 2006 4:58 PM
bruce barker (sqlwork.com)
most dom objects have an innerHTML and innerText, be sue you are spelling
them correctly as javascript is case sensitive.

<table>
<tr >
    <td onclick="alert(this.innerText)">r1col1</td>
</tr>
</table>

note: window.event is IE only.

-- bruce (sqlwork.com)



Show quoteHide quote
"Bob" <s**@sdvsd.dc> wrote in message
news:%23YWwn4VkGHA.324@TK2MSFTNGP02.phx.gbl...
>I tried with innerText, innerHtml, text .... alwyas the sale 'undefined'.
> In the code-behind, i have no choice: after the dot after c(i,j) i only
> can
> take 'text'.
> There must be an equivalence between 'text' in code-behind and a property
> in
> html, no?
> ..
>
>
> "Marina Levit [MVP]" <someone@nospam.com> wrote in message
> news:umBCkwVkGHA.1324@TK2MSFTNGP04.phx.gbl...
>> Depending on the type of element it is, it may not have a 'text'
>> property.
>> Try 'innerText' or 'innerHTML' depending on what you want.
>>
>> I recommend you use the following reference for DHTML so you know what
>> you
>> can do with what objects:
>>
> http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects.asp?frame=true
>>
>> "Bob" <s**@sdvsd.dc> wrote in message
>> news:%237cjzqVkGHA.976@TK2MSFTNGP02.phx.gbl...
>> > Hi,
>> >
>> > I have already posted a similar problem but it 's still a problem for
> me,
>> > so
>> > ..
>> >
>> > I define a table in aspx file and Javascript code.
>> > Purpose: when the user clicks on any cell of the table, the text of it
>> > must
>> > be rendered in an Alert.
>> >
>> > <asp:Table ID="table1" runat="server">
>> > </asp:Table>
>> >
>> > <script language="javascript" type="text/javascript">
>> > function tableclick(event)
>> > {
>> > strid=window.event.srcElement.id
>> > alert(strid)                           // this works: i get e.g. 3:2
>> > strid=window.event.srcElement.text}
>> > alert(strid)                          // this doens't work
>> > ("undefined")
>> > }
>> >
>> > I create row and cells in the code-behind:
>> > Dim r As TableRow
>> > Dim c(50, 20) As TableCell
>> > For i = 0 To 50
>> > r = New TableRow()
>> > For j = 0 To 20
>> > c(i, j) = New TableCell()
>> > c(i, j).ID = j & ":" & i.ToString
>> > r.Cells.Add(c(i, j))
>> > Next
>> > Table1.Rows.Add(r)
>> > Next
>> >
>> > c(3, 2).Text = "this is the text i want in Javascript"
>> > ...
>> >
>> > My problem is that  the Alert gives "undefined".
>> > I can get the 'ID' of each cell, why not the 'text'?
>> > I know there is the property 'ClientID' but it's overruled by the ID i
>> > defined in the code, so that's not the point (i think).
>> >
>> > Thanks for help.
>> > Bob
>> >
>> >
>> >
>> >
>> >
>>
>>
>
>
Author
17 Jun 2006 8:40 AM
Bob
Thanks, it works with innerText.


Show quoteHide quote
"bruce barker (sqlwork.com)" <b_r_u_c_e_removeundersco***@sqlwork.com> wrote
in message news:eC9qaYWkGHA.1320@TK2MSFTNGP04.phx.gbl...
> most dom objects have an innerHTML and innerText, be sue you are spelling
> them correctly as javascript is case sensitive.
>
> <table>
> <tr >
>     <td onclick="alert(this.innerText)">r1col1</td>
> </tr>
> </table>
>
> note: window.event is IE only.
>
> -- bruce (sqlwork.com)
>
>
>
> "Bob" <s**@sdvsd.dc> wrote in message
> news:%23YWwn4VkGHA.324@TK2MSFTNGP02.phx.gbl...
> >I tried with innerText, innerHtml, text .... alwyas the sale 'undefined'.
> > In the code-behind, i have no choice: after the dot after c(i,j) i only
> > can
> > take 'text'.
> > There must be an equivalence between 'text' in code-behind and a
property
> > in
> > html, no?
> > ..
> >
> >
> > "Marina Levit [MVP]" <someone@nospam.com> wrote in message
> > news:umBCkwVkGHA.1324@TK2MSFTNGP04.phx.gbl...
> >> Depending on the type of element it is, it may not have a 'text'
> >> property.
> >> Try 'innerText' or 'innerHTML' depending on what you want.
> >>
> >> I recommend you use the following reference for DHTML so you know what
> >> you
> >> can do with what objects:
> >>
> >
http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects.asp?frame=true
Show quoteHide quote
> >>
> >> "Bob" <s**@sdvsd.dc> wrote in message
> >> news:%237cjzqVkGHA.976@TK2MSFTNGP02.phx.gbl...
> >> > Hi,
> >> >
> >> > I have already posted a similar problem but it 's still a problem for
> > me,
> >> > so
> >> > ..
> >> >
> >> > I define a table in aspx file and Javascript code.
> >> > Purpose: when the user clicks on any cell of the table, the text of
it
> >> > must
> >> > be rendered in an Alert.
> >> >
> >> > <asp:Table ID="table1" runat="server">
> >> > </asp:Table>
> >> >
> >> > <script language="javascript" type="text/javascript">
> >> > function tableclick(event)
> >> > {
> >> > strid=window.event.srcElement.id
> >> > alert(strid)                           // this works: i get e.g. 3:2
> >> > strid=window.event.srcElement.text}
> >> > alert(strid)                          // this doens't work
> >> > ("undefined")
> >> > }
> >> >
> >> > I create row and cells in the code-behind:
> >> > Dim r As TableRow
> >> > Dim c(50, 20) As TableCell
> >> > For i = 0 To 50
> >> > r = New TableRow()
> >> > For j = 0 To 20
> >> > c(i, j) = New TableCell()
> >> > c(i, j).ID = j & ":" & i.ToString
> >> > r.Cells.Add(c(i, j))
> >> > Next
> >> > Table1.Rows.Add(r)
> >> > Next
> >> >
> >> > c(3, 2).Text = "this is the text i want in Javascript"
> >> > ...
> >> >
> >> > My problem is that  the Alert gives "undefined".
> >> > I can get the 'ID' of each cell, why not the 'text'?
> >> > I know there is the property 'ClientID' but it's overruled by the ID
i
> >> > defined in the code, so that's not the point (i think).
> >> >
> >> > Thanks for help.
> >> > Bob
> >> >
> >> >
> >> >
> >> >
> >> >
> >>
> >>
> >
> >
>
>