Solved

How to read value from a TH in HTML table with Javascript (NO JQuery, if possible)

Posted on 2016-09-22
3
54 Views
Last Modified: 2016-11-03
I'm trying to get the value from a table (I have no control over this format or naming) and I'd like to grab the value of "Opened Account" in the example below but not sure how to go about it? I'm trying to target "th.rich-table-headercell" but not sure how to grab value?

<table class="rich-table home table" id="startForm:OpenedReviewApps" border="1" cellpadding="0" cellspacing="0">
     <colgroup span="0"></colgroup>
     <thead class="rich-table-thead">
         <tr class="rich-table-header">
             <th class="rich-table-headercell" scope="colgroup">Opened Accounts
             </th>
         </tr>
    </thead>
   <tbody id="startForm:OpenedReviewApps:tb">
        <tr class="rich-table-row rich-table-firstrow">
            <td class="rich-table-cell" id="startForm:Open" style="width:80%"> some data here
            </td>
        </tr> 
    </tbody> 
</table>

Open in new window

0
Comment
Question by:894359
  • 2
3 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41811775
you can try use document.getElementById, like:
<script language="javascript">
    var v = document.getElementById("startForm:Open").innerText;
    alert("Your var = " + v);
</script>

Open in new window

0
 

Author Comment

by:894359
ID: 41811779
Hi Ryan,
there is no "id" on the "th" I'm trying to read:

<th class="rich-table-headercell" scope="colgroup">Opened Accounts</th>

Thanks
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41811789
ooops, miss understood your question, perhaps you can try this instead:

<script language="javascript">
    var ths = document.getElementsByTagName("th");
    for (var i = 0; i < ths.length; i++) {
        if ((ths[i].getAttribute("class") == "rich-table-headercell") && (ths[i].getAttribute("scope") == "colgroup")) {
            var v = ths[i].innerText;
            break;
        }
    }
    alert("Your var = " + v);
</script>

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now