Rich text editor (RTE) and PreSaveAction() in Sharepoint 2010/2007

As you may know PreSaveAction() is manly used to validate controls before the item is saved( new item or edit). Last week we had some requirement where we needed to get some values from the Rich text box on the save of NewForm.aspx page and parse through all the urls in the Rich text box’s text value.

In that case what we did was to execute our code in the PreSaveAction() function to get and parse through the RichTextbox’s value. Firstly, to find all the Html controls on the page we will use a handy function “getTagFromIdentifier“.
Some details about this function from this article are below.

This function finds the HTML element rendered by a given SharePoint FormField control. It takes the following parameters:

tagName – The name of the tag rendered in the form’s HTML
identifier – The string associated with the SharePoint type of the relevant field
title – The value of the relevant HTML tag’s “title” attribute, which also matches the field’s display name

Here’s a partial table of SharePoint column types and their corresponding “identifiers” and “tagNames”:

SharePoint Field Type       identifier       tagName

Single Line of Text                                           TextField                              input

Multiple Lines of Text                                      TextField                              input

Number                                                            TextField                                 input

Currency                                                          TextField                                 input

Choice (dropdown)                                 DropDownChoice                      select

Lookup (single)*                                            Lookup                                  select

So using this Function and the PreSaveAction() we wrote something like below to get to our RTe’s value

function PreSaveAction()
{
var _body = getTagFromIdentifierAndTitle(“input”,”TextField”,”Body”);
var _body = RTE_GetEditorDocument(_body.id);
var _bodyText = _body.body.innerText;
alert(_bodyText);
return true;
}

function getTagFromIdentifierAndTitle(tagName, identifier, title)
{
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++)
{
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == “” || tempString.indexOf(identifier) == tempString.length – len)) {
return tags[i];
}
}
return null;
}