Table Cell InnerHTML and <P> Corruption

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                                                                               <--
                                                                                <--
]
LVL 1
LarryAndroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GwynforWebCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GwynforWebCommented:
this is  better cleaner

cellTxt=table.rows[rowNo].cells[colNo].innerHTML
cellTxt=cellTxt.replace(/<[^><]+>/g,'')
0
LarryAndroAuthor Commented:
Thanks...
0
leos_Commented:
Thanks for the good question - your code included was very helpful to me.
0
LarryAndroAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.