Solved

Access the text inside a SPAN tag with JS

Posted on 2006-07-19
7
1,168 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 500 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

731 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