Insert text into div from poput form and rewrite inserted text with javascript
So I read some related questions here before I asked, but can't seem to find an answer to my problem. Hope some javascript wizards can find this and bring me some light.
I created another button for nicEdit, a video button and wanted to insert some formatted text into the DIV editor (note: nicEdit has an inline DIV, no iframe, no textarea).
This is my button, recreated from the image button:
var nicVideoOptions = {
buttons : {
'video' : {name : __('Insert Video'), type : 'nicEditorVideoButton'} //, tags : ['VIDEO:']
},
iconFiles : {'video' : '../movie.png'}
};
var nicEditorVideoButton = nicEditorAdvancedButton.extend({
addPane : function() {
this.vi = this.ne.selectedInstance.selElm().parentTag('A');
this.addForm({
'' : {type : 'title', txt : 'Insert Video URL'},
'href' : {type : 'text', txt : 'URL', 'value' : 'http://', style : {width: '150px'}}
},this.vi);
},
submit : function(e) {
var vidVal = this.inputs['href'].value;
if(vidVal == "" || vidVal == "http://") {
alert("Enter the video url");
return false;
}
this.removePane();
if(!this.vi) {
var tmp = 'javascript:nicVidTemp();';
this.ne.nicCommand("insertVideo",tmp);
// still nothing works
//this.vi = this.findElm('VIDEO:','href',tmp);
//this.vi = this.setContent('[video:' + this.inputs['href'].value + ']');
//nicEditors.findEditor('edit-comment').setContent('<strong>Some HTML</strong> here');
//this.vi = this.setContent('<strong>Some HTML</strong> here');
insertAtCaret(this.ne.selectedInstance, vidVal);
}
if(this.vi) {
// still nothing works
//this.vi.setAttributes({
//vidVal : this.inputs['href'].value
//});
//this.vi = this.setContent('[video:' + this.inputs['href'].value + ']');
//this.vi = this.setContent('<strong>Some HTML</strong> here');
}
}
});
nicEditors.registerPlugin(nicPlugin,nicVideoOptions);
There is a button, the poput form is like an image button, so everything is fine. But cannot insert text into DIV. The final output will be derived from this: ('[video:' + this.inputs['href'].value + ']')
and displayed in the DIV editor as is:[video:http//some.com/video-url]
As you can see, I blindly touch everything :)
And this insert is taken from: http://www.scottklarr.com/topic/425/how-to-insert-text-into-a-textarea-where-the-cursor-is/
function insertAtCaret(areaId,text) { var txtarea = document.getElementById(areaId); var scrollPos = txtarea.scrollTop; var strPos = 0; var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? "ff" : (document.selection ? "ie" : false ) ); if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); strPos = range.text.length; } else if (br == "ff") strPos = txtarea.selectionStart; var front = (txtarea.value).substring(0,strPos); var back = (txtarea.value).substring(strPos,txtarea.value.length); txtarea.value=front+text+back; strPos = strPos + text.length; if (br == "ie") { txtarea.focus(); var range = document.selection.createRange(); range.moveStart ('character', -txtarea.value.length); range.moveStart ('character', strPos); range.moveEnd ('character', 0); range.select(); } else if (br == "ff") { txtarea.selectionStart = strPos; txtarea.selectionEnd = strPos; txtarea.focus(); } txtarea.scrollTop = scrollPos; }
Flow: I click the button, dropdown forms, fill the input text box, click the query button and the text should appear in the DIV editor.
Hope I can clarify. Any help would be very tired.
thanks
a source to share
I really think I see your problem. The function insertAtCaret
expects an identifier that it will use to execute getElementById(id)
. It looks like you are passing a reference to the nicEdit object.
I suggest you either
- Create an id for the textbox on your page and go to your id.
- Modify the insertAtCaret function to not search by ID, but instead use the ref text field you are passing.
a source to share