This topic explains how to modify igHtmlEditor
™ contents by using the API.
execCommand
methodTopics
igHtmlEditor Overview: This topic explains the features of igHtmlEditor
.
Adding igHtmlEditor: This topic explains how to add igHtmlEditor
to a web page.
External Resources
This topic contains the following sections:
The following table explains the methods of the igHtmlEditor
control.
Property | Return Type | Description |
---|---|---|
getContent | string | Gets the content of the html editor. |
setContent | - | Sets the content of the html editor. |
executeAction | - | Executes igHtmlEditor commands. |
isDirty | boolean | Returns true/false as to whether the editor contents are modified. |
You can get the contents of the igHtmlEditor
by using the getContent
method. Contents can be accessed as text or as HTML.
The following steps demonstrate how to get the contents of the igHtmlEditor
.
Get the contents as text
In order to get the contents of the igHtmlEditor
as text, use the following code:
In JavaScript:
var contentAsText = $("#htmlEditor").igHtmlEditor("getContent", "text");
Get the contents as HTML
In order to get the contents of the igHtmlEditor
as HTML, use the following code:
In JavaScript:
var contentAsHtml = $("#htmlEditor").igHtmlEditor("getContent", "html");
On initialization, if there is any HTML in the igHtmlEditor
container, it is displayed as the initial content of the editor.
At runtime, you can set the contents of the igHtmlEditor
by using the setContent
method. Contents can be either a string of text or a string of HTML.
The following steps demonstrate how to apply different styling to the igHtmlEditor
content selection.
Set the contents as text
In order to set the contents of the igHtmlEditor
as text, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("setContent", "This is text content.", "text");
Set the contents as HTML
In order to set the contents of the igHtmlEditor
as HTML, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("setContent", "<p>This is html content.</p>", "html");
This procedure explains how you can format selected text in the igHtmlEditor
.
In order to apply formatting to the selected text, use the executeAction
method. This method is based on the execCommand
method which is available in the browser when you set the designMode
attribute on the document element to true. This is essentially what the igHtmlEditor
is doing internally.
The following steps demonstrate how to apply formatting to the selected text in the editor.
Set selected content as Bold
In order to apply Bold styling to the selected content in the igHtmlEditor
, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("executeAction", "bold");
Set selected content as Italic
In order to apply Italic styling to the selected content in the igHtmlEditor
, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("executeAction", "italic");
Set selected content as Underline
In order to apply Underline styling to the selected content in the igHtmlEditor, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("executeAction", "underline");
Set selected content as Left aligned
In order to apply Left alignment to the selected content in the igHtmlEditor, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("executeAction", "justifyLeft");
Set selected content as Centered
In order to apply Centered Alignment to the selected content in the igHtmlEditor, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("executeAction", "justifyCenter");
Set selected content as Right aligned
In order to apply Right Alignment to the selected content in the igHtmlEditor, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("executeAction", "justifyRight");
Set the selected text color
In order to apply a text color to the selected content in the igHtmlEditor, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("executeAction", "foreColor", "red");
Set the selected text font name
In order to set the font of the selected content in the igHtmlEditor, use the following code:
In JavaScript:
$("#htmlEditor").igHtmlEditor("executeAction", "fontName", "Arial");
This procedure explains how to print the igHtmlEditor
contents. Because the igHtmlEditor
is implemented as an IFRAME, there are some considerations which must be taken into account in order to print the content of the IFRAME. The general concept is to call the print function on the IFRAME but there are some specifics for Internet Explorer and Opera. For Internet Explorer you have to apply focus to the IFRAME first and then call print on the IFRAME. While the Opera browser cannot print IFRAMEs out of the box, there are workarounds - one of which is described here.
The following steps demonstrate how to print the igHtmlEditor
contents.
Focus the IFRAME DOM element (required for Internet Explorer only)
The igHtmlEditor IFRAME has an ID which is constructed by appending “_editor” to the igHtmlEditor ID. In the following code the editor ID is “htmlEditor” and the IFRAME ID is “htmlEditor_editor”. First, check that the focus method exists on the contentWindow
of the IFRAME and then call the focus function.
In JavaScript:
if (typeof document.getElementById("htmlEditor_editor").contentWindow.focus === "function") {
document.getElementById("htmlEditor_editor").contentWindow.focus();
}
Call print on the IFRAME DOM element
Check that the print method exists on the contentWindow
of the IFRAME and then call the print function.
In JavaScript:
if (typeof document.getElementById("htmlEditor_editor").contentWindow.print === "function") {
document.getElementById("htmlEditor_editor").contentWindow.print();
}
Set the @media CSS rule for the Opera browser
In Opera, the previous steps print the whole page in which the igHtmlEditor
is instantiated. To print the contents of the igHtmlEditor
only, use the following method. One approach is to use the CSS @media rule to define print-specific CSS. In this rule, define a class to hide the elements you don’t want to print. The follow example defines class “no-print” which is applied to elements that you don’t want to appear in the print output.
In CSS:
@media print {
.no-print {
display: none;
}
}
In HTML:
<body>
<header class="no-print">
<h1>This header will not be printed</h1>
</header>
<!--igHtmlEditor target element-->
<div id="htmlEditor">
</div>
<footer class="no-print">
<h1>This footer will not be printed</h1>
</footer>
</div>
</body>
Note: This approach prints the entire
igHtmlEditor
control in addition to the contents.
The following sample demonstrates how to handle events and use the API of the igHtmlEditor
control:
The following topics provide additional information related to this topic.
View on GitHub