-1

I am trying to identify the texts from CKeditor.

The ckData contains html markup and tables from Ckeditor that is input from users.

For example

text here texts here texts here
second line texts...etc.
<br>
<table>
  ...
</table>

more texts here...

<table>
....
</table>

I want be able to get all texts from the ckData.

I can get table texts with the following codes

   var contents = {};
    var temp = document.createElement('div');
    var instance = this;
    temp.innerHTML = ckData;

    var tables = temp.getElementsByTagName('table')
    //use tables as array...
    for(var i = 0; i<tables.length; i++){
        var table = tables[i];
        contents.rowsCount = $('tr', table).length;
        contents.columnsCount = $('td', table).length / question.rowsCount;
        contents.texts='';

        $(table).find('td').each(function(){
                contents.push($(this).text().trim());
                contents.texts += $(this).text()
        })
    }

I want to be able to extract texts outside of table WHILE maintain the original order of the ckData.

so the final contents variable I was hoping to get is like:

first part of texts

table cell data //got this

second part of texts

table cell data //got this

I got the table cell data but I am not sure how to get the texts outside of table structure.

I hope I explain it well. Thank you so much for the help!

FlyingCat
  • 14,036
  • 36
  • 119
  • 198
  • Why not just use `temp.textContent` (note that you will lose formatting)? – Paul S. Jul 17 '13 at 23:57
  • @PaulS. thanks, but I need to maintain the html markup for texts outside of table. – FlyingCat Jul 17 '13 at 23:59
  • possible duplicate of [How to get text inside of container that is not part of children](http://stackoverflow.com/questions/12819953/how-to-get-text-inside-of-container-that-is-not-part-of-children)...which is ironically, a possible duplicate of [How do I select text nodes with jQuery?](http://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery)...duplicateception – Ohgodwhy Jul 18 '13 at 00:02
  • 1
    Then as you're already using a `temp` parent; convert the table text to a TextNode `var text = document.createTextNode(tableText)`, at the end inside each loop do `tables[i].parentNode.insertBefore(text, tables[i]);` then remove the table `tables[i].parentNode.removeChild(tables[i])`, so you effectively replace your `` elements with their text, then you can keep all html markup by converting the contents of `temp` to string in your favourite way.
    – Paul S. Jul 18 '13 at 00:05
  • you can just remove the tables and grab the .innerText from the common container. – dandavis Jul 18 '13 at 00:07
  • @PaulS. and Dandavis thanks guys, I will give it a try. – FlyingCat Jul 18 '13 at 00:41

1 Answers1

0

You can simple use .text() function in jQuery. It returns just the text and trims all the markup code.

defau1t
  • 10,593
  • 2
  • 35
  • 47