Solved

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

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

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 50

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

832 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