Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Access the text inside a SPAN tag with JS

Posted on 2006-07-19
7
Medium Priority
?
1,171 Views
Last Modified: 2008-01-09
Hi Experts,

how do I get the text inside a SPAN tag that resides within <TD> tags.  Below is the HTML and my attempt at JavaScript:

HTML:
<td>
      <span id="dgAssignedOrders__ctl2_lblDateAssigned" Readonly="true">7/11/2006</span>
</td>
<td>
      <span id="dgAssignedOrders__ctl3_lblDateAssigned" Readonly="true">7/12/2006</span>
</td>

Here is JavaScript that is being called when pressing a button:
function CheckForExistingDates()
{
      var doc = document;
      var table = doc.getElementById('dgAssignedOrders');  
      var rows = table.rows;
      var isFound = false;

      for(var i = 1; i < rows.length; i++)
      {
            var cells = rows[i].getElementsByTagName("TD");
            var curTD = cells[0];
            var curSpanDate = curTD.getElementsByTagName("SPAN");
            alert(curSpanDate.innerHTML);  <----I would like a messgebox to appear with 7/11/2006, etc.
      }      
      return false;
}

0
Comment
Question by:brdrok
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 10

Expert Comment

by:0h4crying0utloud
ID: 17141968

you might need to reference the first element of the curSpanDate array like so:

function CheckForExistingDates()
{
     var doc = document;
     var table = doc.getElementById('dgAssignedOrders');  
     var rows = table.rows;
     var isFound = false;

     for(var i = 1; i < rows.length; i++)
     {
          var cells = rows[i].getElementsByTagName("TD");
          var curTD = cells[0];
          var curSpanDate = curTD.getElementsByTagName("SPAN");
          alert((curSpanDate[0]).innerHTML);  
     }    
     return false;
}
0
 
LVL 6

Expert Comment

by:inviser
ID: 17141981
Here is how you access the HTML inside the span tag:

<td>
     <span id="dgAssignedOrders__ctl2_lblDateAssigned" Readonly="true">7/11/2006</span>
</td>
<td>
     <span id="dgAssignedOrders__ctl3_lblDateAssigned" Readonly="true">7/12/2006</span>
</td>

<script language="javascript">
var htmlInsideSpan = document.getElementById("dgAssignedOrders__ctl2_lblDateAssigned").innerHTML;

alert(htmlInsideSpan);
</script>
0
 
LVL 29

Accepted Solution

by:
Pravin Asar earned 2000 total points
ID: 17142076
<table id="dgAssignedOrders">
<tr>
<td>HH</td></td>HHH</td>
</tr>

<tr>
<td>
     <span id="dgAssignedOrders__ctl2_lblDateAssigned" Readonly="true">7/11/2006</span>
</td>
<td>
     <span id="dgAssignedOrders__ctl3_lblDateAssigned" Readonly="true">7/12/2006</span>
</td>
</tr>
</table>

Here is JavaScript that is being called when pressing a button:
<script language="javascript">
function getParent (src, tagName) {
     while (src.parentNode != null) {
          if (src.parentNode.tagName  == tagName) {
               return src.parentNode;
          }
          src = src.parentNode;
     }
     return src;
}
function CheckForExistingDates()
{
       var spans = document.getElementsByTagName('SPAN');
       for (sx=0; sx < spans.length; sx++) {
        var tdObj = getParent(spans[sx], "TD");
        if (tdObj) { alert (spans[sx].innerHTML); }

       }
     return false;
}
CheckForExistingDates();
</script>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Pravin Asar
ID: 17142152
If you happen to have table with many rows/columns , get the SPAN object first and  then determining the
parent (== TD) will be strategy of adopt.

This way will be faster than walking the the table's DOM.



0
 
LVL 7

Author Comment

by:brdrok
ID: 17145594
prvinasar,

your suggestion works like a charm.  In the spirit of learning, could you please explain some of your syntax please.  The part that confuses me is:

if (tdObj)
is that the same as if(tdObj != null)?

Do all objects (well, most of them anyways) have a property called "tagName"?  So, if I had a variable that references an HTML table, the tagName would be "TABLE" without the "<" and ">" brackets?

once again,
thank you very much.
0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 17146384
if (tdObj)
is that the same as if(tdObj != null)?

Yes. Both statement mean same thing.
TagName is a tag with < and >  and it is uppercase.


for Table , a tagName is TABLE

0
 
LVL 29

Expert Comment

by:Pravin Asar
ID: 17146540
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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 basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

647 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