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 = {}; $("td.ms-formbody").each(function(){ var myMatch = $(this).html().match(/FieldName="(.+)"s+FieldInternalName="(.+)"s+FieldType="(.+)"s+/); if(myMatch!=null){ // Display name var disp = myMatch[1]; // FieldInternalName var fin = myMatch[2]; // FieldType var type = myMatch[3]; if(type=='SPFieldNote'){ if($(this).find('script').length>0){ type=type+"_HTML"; } } if(type=='SPFieldLookup'){ if($(this).find('input').length>0){ type=type+"_Input"; } } // Build object res[fin] = this.parentNode; $(res[fin]).attr('FieldDispName',disp); $(res[fin]).attr('FieldType',type); } }); return res; } </script>
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($(fields['Title']).attr('FieldDispName')); // Alert the FieldType for the "Title field" alert($(fields['Title']).attr('FieldType'));
Regards
Alexander
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.
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:
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 🙂
Alexander
How would I get the SelectResult or Candidate using this function?