0

I'm in a SharePoint Display form and want to do some calculations on the client's side to populate a blank single line of text field.

In an edit form, to set and select my value I have something like this:

$("input[title='My Field Here']").value(time);

But the display form seems to hold values a lot differently (in a table). How would I go about selecting a value from my field when it's stored like this with (seemingly) no unique identifiers:

<td width="450px" valign="top" id="SPFieldDateTime" class="ms-formbody">
        <!-- FieldName="MyDate"
             FieldInternalName="MyDate"
             FieldType="SPFieldDateTime"
          -->
            3/26/2014

        </td>

I don't think I can select anything based on the comment that's there (would be nice..), but if all of my fields are contained within "td" and there's more than one "SPFieldDateTime", how can I select this specific field's value of 3/26/2014?

edit:

I left this part out which appears just above the above line.

<a name="SPBookmark_MyDate"></a>
5

Using jQuery, find the nearest item with a unique identifier. Not all fields in a form have unique IDs, as you found with the date fields, which all have the id="SPFieldDateTime". The corresponding label column, however, contains the field name and this one must be unique.

enter image description here

Use this as your hook. You can then go to the sibling of the unique element with the class name "ms-formbody". For example:

$('td.ms-formlabel:contains("Text2")').siblings(".ms-formbody").css("background", "yellow");

will show

enter image description here

Or to retrieve the text value use something like

$('td.ms-formlabel:contains("Text2")').siblings(".ms-formbody").text() 

for example in an alert:

alert($('td.ms-formlabel:contains("Text2")').siblings(".ms-formbody").text()); 

enter image description here

Or hide the row altogether with

$('td.ms-formlabel:contains("Text2")').parent().hide();
  • Thank you very much. I've followed a number of your answers and you are always above and beyond helpful. I can't use the + rep thing yet, but I will come back when I can. edit: I got a few points from marking this as answer so I did it now. – Dinerdo Mar 19 '14 at 2:27
  • Note: Please make sure you thoroughly test this code on your forms. As an example Text22 will also be selected if you use the code as is. That's probably not what you want. FYI, you can use the comment to find form rows. Take a look at the jQuery#contents method. – iOnline247 Mar 21 '14 at 11:48
  • @iOnline247, fair comment. Could you please provide some detail about exactly HOW to use the jQuery#contents method? – teylyn Mar 22 '14 at 4:44
  • Are you looking for something more than this? api.jquery.com/contents – iOnline247 Mar 24 '14 at 17:45
  • 1
    Yes. I'd like to see the actual line of code in the above context instead of a link to the docu. It was your idea, so show how it will work. Imagine that there are people who may not know what you know. Share your insights. If you don't want to do that, why are you here? – teylyn Mar 24 '14 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.