[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Table Cell InnerHTML and <P> Corruption

Posted on 2004-10-28
5
Medium Priority
?
1,627 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
Comment
Question by:LarryAndro
  • 2
  • 2
5 Comments
 
LVL 31

Accepted Solution

by:
GwynforWeb earned 2000 total points
ID: 12436545
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
ID: 12436607
this is  better cleaner

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

Author Comment

by:LarryAndro
ID: 12441270
Thanks...
0
 
LVL 3

Expert Comment

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

Author Comment

by:LarryAndro
ID: 13816887
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

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

611 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