Get Table Cell value using Javascript

Discussion in 'ASP .Net' started by =?Utf-8?B?Qw==?=, Apr 23, 2007.

  1. How do I get the text from my cell e..g. <td>Test Text</td>



    var elTableCells = elTableRow.getElementsByTagName("td");

    alert(elTableCells[0].innerText);
     
    =?Utf-8?B?Qw==?=, Apr 23, 2007
    #1
    1. Advertising

  2. =?Utf-8?B?Qw==?=

    Ray Costanzo Guest

    You answered your own question. What you're doing below should work, as
    long as elTableRow is defined prior to that call. Sample:

    <html><body>
    <script type="text/javascript">
    function test() {
    var elTableRow = document.getElementById("somerow");
    var elTableCells = elTableRow.getElementsByTagName("td");
    alert(elTableCells[0].innerText);
    }
    </script>
    <button onclick="test()">click me</button>
    <table>
    <tr id="somerow">
    <td>cell 1</td>
    <td>cell 2</td>
    </tr>
    </table>
    </body>
    </html>




    "C" <> wrote in message
    news:...
    > How do I get the text from my cell e..g. <td>Test Text</td>
    >
    >
    >
    > var elTableCells = elTableRow.getElementsByTagName("td");
    >
    > alert(elTableCells[0].innerText);
    >
     
    Ray Costanzo, Apr 23, 2007
    #2
    1. Advertising

  3. Hi,
    Try this code:
    <td id ="tdname">Test Text</td>
    alert(document.forms[0].getElementsByTagId("tdname").innerText);
    you need to take care of case-sensitivity as javascript is
    case-sensitive(that i have not taken care in above code)
    Hope this helps
    --
    If my answer helped you,then please do press Yes below.
    Thanks and Regards.
    Manish Bafna.
    MCP and MCTS.



    "C" wrote:

    > How do I get the text from my cell e..g. <td>Test Text</td>
    >
    >
    >
    > var elTableCells = elTableRow.getElementsByTagName("td");
    >
    > alert(elTableCells[0].innerText);
    >
     
    =?Utf-8?B?TWFuaXNoIEJhZm5h?=, Apr 23, 2007
    #3
  4. =?Utf-8?B?Qw==?=

    Leda Wu Guest

    If the cell is a dropdown list, how can I get the selectedIndex value by this approach?
    Thank you very much!

    EggHeadCafe.com - .NET Developer Portal of Choice
    http://www.eggheadcafe.com
     
    Leda Wu, Apr 30, 2007
    #4
  5. =?Utf-8?B?Qw==?=

    Leda Wu Guest

    get table cell value using javascript when cell is adropdown list

    If the cell is a dropdown list, how can I get the selectedIndex value by this approach?
    Thank you very much!

    EggHeadCafe.com - .NET Developer Portal of Choice
    http://www.eggheadcafe.com
     
    Leda Wu, Apr 30, 2007
    #5
  6. =?Utf-8?B?Qw==?=

    oceans_ripple_light

    Joined:
    Jul 25, 2012
    Messages:
    1
    Getting value from HTML table with JS

    You can get cell value with JS even when click on the cell:

    Code:
    .......................
        
              <head>
                   
                <title>Search students by courses/professors</title>
                 
                <script type="text/javascript">
                  
                function ChangeColor(tableRow, highLight)
                {
                   if (highLight){
                	   tableRow.style.backgroundColor = '00CCCC';
                   }
                
                else{
                	 tableRow.style.backgroundColor = 'white';
                    }   
              }
            
              function DoNav(theUrl)
              {
              document.location.href = theUrl;
              }
              </script>
            
            </head>
            <body>
            
                 <table id = "c" width="180" border="1" cellpadding="0" cellspacing="0">
                
                        <% for (Course cs : courses){ %>
                  
                        <tr onmouseover="ChangeColor(this, true);" 
                            onmouseout="ChangeColor(this, false);" 
                            onclick="DoNav('http://localhost:8080/Mydata/ComplexSearch/FoundS.jsp?courseId=<%=cs.getCourseId()%>');">
                     
                             <td name = "title" align = "center"><%= cs.getTitle() %></td>
                                   
                        </tr>
                       <%}%>
            
            ........................
            </body>
    I wrote the HTML table in JSP.
    **Course** is is a type. For example Course cs, cs= object of type Course which had 2 attributes: id, title.
    **courses** is an ArrayList of Course objects.

    The HTML table displays all the courses titles in each cell. So the table has 1 column only:
    Course1
    Course2
    Course3
    ......
    Taking aside:

    Code:
     onclick="DoNav('http://localhost:8080/Mydata/ComplexSearch/FoundS.jsp?courseId=<%=cs.getCourseId()%>');"
    This means that after user selects a table cell, for example "Course2", the title of the course- "Course2" will travel to the page where the URL is directing the user:
    Code:
    http://localhost:8080/Mydata/ComplexSearch/FoundS.jsp
    . "Course2" will arrive in FoundS.jsp page. The identifier of "Course2" is courseId. To declare the variable courseId, in which CourseX will be kept, you put a "?" after the URL and next to it the identifier.
    It works.
     
    oceans_ripple_light, Jul 25, 2012
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phillip Roncoroni
    Replies:
    14
    Views:
    1,143
    Toby A Inkster
    Apr 5, 2004
  2. =?Utf-8?B?Qw==?=

    Get Table Cell Value using Javascript

    =?Utf-8?B?Qw==?=, Apr 23, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    4,335
    Cowboy \(Gregory A. Beamer\)
    Apr 23, 2007
  3. yukatan

    get a table cell value

    yukatan, Nov 6, 2003, in forum: Javascript
    Replies:
    2
    Views:
    254
    Lasse Reichstein Nielsen
    Nov 6, 2003
  4. harry

    Get 2nd cell value from table row?

    harry, Jul 22, 2004, in forum: Javascript
    Replies:
    2
    Views:
    216
    Mick White
    Jul 22, 2004
  5. Sunny

    get Table cell value

    Sunny, Oct 20, 2008, in forum: Javascript
    Replies:
    3
    Views:
    492
    Martin Honnen
    Oct 20, 2008
Loading...

Share This Page