Revised function init_fields()

19.08.2011 Updated the function to support new versions of Firefox.

I got a tip from Alexey Krasheninnikov regarding using regex rather then “indexOf” and “substring” in the function “init_fields()”. It is both quicker and cleaner.

This function is used in all my scripts that works with NewForm, DispForm or EditForm. It only works in non-modified forms.

Here is a revised version of the function. The regex is supplied by Alexey Krasheninnikov, my regex knowledge is not quite there, but I’m learning.
<script type="text/javascript" src="../../Javascript/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
fields = init_fields();

function init_fields(){
var res = {};
var myMatch = $(this).html().match(/FieldName="(.+)"s+FieldInternalName="(.+)"s+FieldType="(.+)"s+/);
// Display name
var disp = myMatch[1];
// FieldInternalName
var fin = myMatch[2];
// FieldType
var type = myMatch[3];
// Build object
res[fin] = this.parentNode;
return res;

To address a field you use the same method as before:
// Get the table row containing the "Title field"
var titleFieldTR = $(fields[‘Title’]);

I have added the fields “DisplayName” and the “FieldType” as attributes as it might come in handy:
// Alert the DisplayName for the "Title field"
// Alert the FieldType for the "Title field"


5 thoughts on “Revised function init_fields()”

  1. Thanks for the credit.

    This approach with a different regex can be seen in the Imtech Extended Lookup by Waldek Mastykarz.

    You might want to find a very handy free tool in your learning called RegexBuilder. If you don’t find it, contact me, I’ll try to help.

  2. Hi Alexander,

    I think we should respect Firefox users and make a correction:

    firstChild for them would be “n ” that doesn’t conform to the Regular Expression. We must therefore iterate the nodes until we find the comment node: nodeType==8:

     var commentNode;
     $(fieldWrapper.parent("").context.childNodes).each(function() { if (this.nodeType == 8) commentNode = this; }); 
     var text = commentNode.nodeValue;
    1. Hi,
      I have tested it in Firefox prior to posting it, and it does what it is supposed to do.

      I can’s see why we need to identify the correct node, as all we want is the string value for “FieldInternalName”, DisplayName” and “FieldType”.

      From the <TD> we find the parent <TR> which is the “node” we want.

      Please correct me if I’m wrong 🙂


Leave a Reply