Solved

Table Cell InnerHTML and <P> Corruption

Posted on 2004-10-28
1,605 Views
Last Modified: 2012-05-05
I am looping thru a table, cell by cell, evaluating the innerHTML-derived contents.  Everything works fine for simple cells...  <td>B</td>.  But, when I come to a <td><p ....>B</p></td> cell, containing <p>...</p>, strange things happen.  But, definitely, 'B' is not returned!  

How can I get innerHTML to return 'B' only?

Just in case the above question doesn't make sense, I'm including below my HTML I created to test this problem...

<html>
<head>

<script>

      function MakeSubArray(tableNm){
            document.write("<br>TABLE: "+tableNm);
            var table, rowNo, colNo, cellTxt;
            var tableArr = new Array();
            var table=document.getElementById(tableNm);
            var tableLen=table.rows.length;
            for(rowNo=0;rowNo<tableLen;rowNo++){
                  document.write("     row#:"+rowNo+"        ");
                  for(colNo=0;colNo<2;colNo++){
                        document.write(".");
                        cellTxt=table.rows[rowNo].cells[colNo].innerHTML;
                        document.write("   cell#:"+colNo+"["+cellTxt+"]");
                  }                  
            }      
        }
        
        
</script>

</head>
<body>

'table1' here...
<table id="table1" >
      <tr>
            <td>A</td>
            <td>B</td>
      </tr>
</table>

<br>'table2' here...
<table id="table2" >
      <tr>
            <td>A</td>
            <td><p>B</p></td>
      </tr>
</table>

<br><br>Calling MakeSubArray to print cell contents...

<script>
      MakeSubArray("table1");
      MakeSubArray("table2");
</script>

</table>
</body>
</html>

Running the above gives...

'table1' here...
A B

'table2' here...
A B
 
Calling MakeSubArray to print cell contents...
TABLE: table1 row#:0 . cell#:0[A]. cell#:1[B]               <--B prints fine for table1...
TABLE: table2 row#:0 . cell#:0[A]. cell#:1[                  <--notice cell#1 corruption..
B                                                                               <--
                                                                                <--
]
0
Question by:LarryAndro
    5 Comments
     
    LVL 31

    Accepted Solution

    by:
    this is not corruption the innerHTML is being extractes ok, but when you do a document.write the output formated as if it was html so 1[<p>B</p>] gets displayed as

    1[

    B
                                                                                 
    ]

    which is what you would expect. You must remove the html tags this will clean it

    cellTxt=table.rows[rowNo].cells[colNo].innerHTML
    cellTxt=cellTxt.match(/>[^><]+</)[0].replace(/[><]/g,'')
    0
     
    LVL 31

    Expert Comment

    by:GwynforWeb
    this is  better cleaner

    cellTxt=table.rows[rowNo].cells[colNo].innerHTML
    cellTxt=cellTxt.replace(/<[^><]+>/g,'')
    0
     
    LVL 1

    Author Comment

    by:LarryAndro
    Thanks...
    0
     
    LVL 3

    Expert Comment

    by:leos_
    Thanks for the good question - your code included was very helpful to me.
    0
     
    LVL 1

    Author Comment

    by:LarryAndro
    I depend so much on the questions on this site.  Good to know that one of my questions, and the answer, was useful to someone else!
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    This article shows how to read a single webpage's HTML into a string variable, and it also shows how to automate a sequence so you can read and process a list of webpages.  I was tasked with reading and parsing a series of webpages to collect some s…
    In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
    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…

    846 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

    10 Experts available now in Live!

    Get 1:1 Help Now