Solved

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

Posted on 2016-09-22
3
42 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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 embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

743 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

15 Experts available now in Live!

Get 1:1 Help Now