I am creating a dynamic form with a wysiwig editor nice editor which goes like this.
Scripts needed
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="http://js.nicedit.com/nicEdit-latest.js" type="text/javascript"></script>
<script type="text/javascript">bkLib.onDomLoaded(nicEditors.allTextAreas);</script>
Html
<div id="p_scents">
<p>
<input type="text" name="title">
<textarea name="description[]" rows="20" cols="80"></textarea>
</p>
</div>
And the jquery function
$(function () {
var editors = {}; //Keep track of all added nicEditors for removal later
var scntDiv = $('#p_scents');
$(document).on('click', '#addScnt', function () {
var elm = $('<div class="con"><input type="text" id="Text1" value="" /></div>').appendTo(scntDiv);
var elm = $('<textarea NAME="description[]"></textarea>').appendTo(scntDiv); // Add the textarea to DOM
var curSize = $('textarea[name="description[]"]').length; //Get the current SIZE of textArea
editors[curSize] = new nicEditor().panelInstance(elm[0]); //Set the Object with the index as key and reference for removel
elm.after($('<a/>', { //Create anchor Tag with rel attribute as that of the index of corresponding editor
rel: curSize,
'class': "remScnt",
text: "Remove",
href: '#'
})).next().andSelf().wrapAll($('<p/>')); //add it to DOM and wrap this and the textarea in p
});
$(document).on('click', '.remScnt', function (e) {
e.preventDefault();
var elem = $(this).prev('textarea'); //Get the textarea of the respective anchor
var index = this.rel; //get the key from rel attribute of the anchor
editors[index].removeInstance(elem[0]); //Use it to get the instace and remove
delete editors[index]; //delete the property from object
$(this).closest('con').remove();
$(this).closest('p').remove(); //remove the element.
});
});
This script is taken from this stack overflow question. NicEdit html editor for managing dynamic add/remove textareas
With this scrip i want to attach a text field as title the above script is creating the same but once it is created if i press on remove button it is not getting removed only the text area is getting removed where am i going wrong how can i fix this?
Updated fiddle is here http://jsfiddle.net/eEUEW/34/
you can use .parent()
and .prev()
replace
$(this).closest('con').remove();
$(this).closest('p').remove();
with
$(this).parent().prev().remove();
$(this).closest('p').remove();