Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Table Cell InnerHTML and <P> Corruption

Posted on 2004-10-28
5
Medium Priority
?
1,623 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
[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
  • 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

610 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