I’ve a table structure where a CKEditor is attached to a table td. Something like this:
HTML:
<table class="table table-striped table-condensed" id="questionDetails">
<thead>
<tr>
<th>Question Name</th>
<th>Options</th>
<th>Answers</th>
</tr>
</thead>
<tbody id="tbody">
<tr>
<td class="col-xs-3">
<input type="text" class="txtQuestionName form-control" name="txtQuestionName" value="" />
</td>
<td class="col-xs-3">
<input type="text" class="txtOptions form-control" name="txtOptions" value="" />
<span class="addOptions">(+)</span>
</td>
<td class="col-xs-3">
<input type="text" class="txtAnswers form-control" value="" />
<span class="addAnswers">(+)</span>
</td>
<td class="col-xs-12">
<input type="text" class="txtExplanation form-control editor1" name="editor1" />
</td>
<td>
<a href="javascript:void(0);" id="btnAdd" class="btn btn-primary">Add</a>
</td>
</tr>
</tbody>
</table>
jQuery:
<script src="~/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
//Initialize CKEditor by giving id of text area
CKEDITOR.replace('editor1');
//Get each instance of CKEditor
for (instance in CKEDITOR.instances) {
//update element
CKEDITOR.instances[instance].updateElement();
}
//Add row to the table
$("#btnAdd").click(function () {
$("#questionDetails").append('<tr><td><input type="text" class="txtQuestionName form-control" class="form-control" /></td> <td><input type="text" class="txtOptions form-control" /><span class="addOptions">(+)</span></td> <td><input type="text" class="txtAnswers form-control" /> <span class="addAnswers">(+)</span></td><td><input type="text" class="txtExplanation form-control editor1" name="editor1" /></td> <td><a href="javascript:void(0);" class="btn btn-danger remCF">Remove</a></td> </tr>');
});
</script>
The above code works when the page is loaded by default. But when I try to add more rows to the table dynamically using jQuery, that doesn’t create or assign the CKEditor to that input element. The other input elements are created except the editor. Is there any way I can assign the CKEditor to the dynamically created rows in the table and get all the values from it?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Here is a working example. You have to make sure to give all editors a unique ID
<div>
<textarea id="editor1"></textarea>
</div>
<div>
<button type="button" id="btnAdd">Add CKEditor</button>
</div>
<div id="questionDetails">
</div>
<script type="text/javascript" src="~/ckeditor/ckeditor.js"></script>
<script type="text/javascript">
//the inital editor
CKEDITOR.replace('editor1');
//use an number to make the id's of the new editors unique
var index = 2;
//Add row to the table
$("#btnAdd").click(function () {
//create the new id
var newEditor = 'editor' + index;
//append some html
$("#questionDetails").append('<textarea id="' + newEditor + '"></textarea>');
//init the new editor
CKEDITOR.replace(newEditor);
//increment
index++;
});
</script>
If you want to read the contents, simply loop the instances and get the data with getData()
$("#btnRead").click(function () {
for (var i in CKEDITOR.instances) {
alert(CKEDITOR.instances[i].getData());
}
});
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0