Refferencing SharePoint form fields with JavaScript

July 17th, 2007 | Categories: SharePoint, SharePoint administration | Tags:

Sometimes you need to refference SharePoint form fields for manipulation. I usually use JavaScript. With SharePoint 2.0 you can refference a form field with

document.getElementsByName(urn:schemas-microsoft-com:office:office#Field_Name)[0]

where Field_Name is the SharePoint name of the field (you can find it at the end of URL when viewing properties of the field under “Modify Settings and columns”. For example if I’d need to change the value of a form field Title, I’d use the following code:

document.getElementsByName(‘urn:schemas-microsoft-com:office:office#Title’)[0].value = ‘Boris’

Thus changing the value to Boris.

The story gets a bit more complicated with SharePoint 3.0. The names of fields now also contain ID of the form, which is constantly changing. The previous case in one of our forms would be

document.getElementsByName(‘ctl00$m$g_740df035_0c04_4906_89d7_cb38429413df$ctl00$ctl04$ctl00$ctl00$ctl00$ctl04$ctl00$ctl00$TextField’)[0].value = ‘Boris’

The problem occurs with changing ID of the form so you can’t (at least that I know of) refference the form field value by name alone.

The solution here is using form titles. I’ve prepared a small javascript to get the form field by its type and title.

function getField(fieldType,fieldTitle) {
    var docTags = document.getElementsByTagName(fieldType);
    for (var i=0; i < docTags.length; i++) {
        if (docTags[i].title == fieldTitle) {
            return docTags[i]
        }
    }
}

Using this script now you can change the value of a title field with

getField(‘input’,'Title’).value = ‘Boris’

  1. presack
    August 15th, 2007 at 15:40
    Reply | Quote | #1

    Web BorG-

    Nice post. I seemed to have more luck for some reason using getElementByID, but with the same “urn:schemas…” syntax that you blogged about.

    However, I have not found any way to reference a choice (dropdown selection) field for some reason. The page will load without any errors, but I can not even get the control to fire a function that has a simple alert statement in it.

    What I am trying to do is store the results of a selection of a dropdown choice box any time it is changed to a cookie, and then add script to automatically select that choice in the future.

    Any help is greatly appreciated!

    Thanks,
    presack

  2. Hari
    August 21st, 2007 at 21:39
    Reply | Quote | #2
  3. Markuso
    February 6th, 2008 at 12:30
    Reply | Quote | #3

    Great job Presack.

    I’ve also found a way to get a reference to a Radio Buttons Choice field type that was tricky to get at since it doesn’t use the ‘Title’ attribute. Please see below:

    http://blog.markuso.com/posts/9/using-javascript-to-access-a-list-form-field-in-sharepoint-designer/

  4. Erik
    February 14th, 2008 at 23:46
    Reply | Quote | #4

    Great post, got it working easily enough, but i have an issue.

    Has anyone figured out how to access the time choice fields from a date/time field?

    I can access it if it is just the date, but I haven’t figured out how to access the time part of it.

  5. Boris Gomiunik
    February 15th, 2008 at 10:24
    Reply | Quote | #5

    Hi, Erik.

    You can use the IE developer Toolbar to get the ID or Title of the field:

    http://webborg.blogspot.com/2007/10/great-tools-for-web-developers-pt1.html

  6. Anonymous
    February 18th, 2008 at 09:09
    Reply | Quote | #6

    My requirement is that I need to create a custom column of type ‘choice’ for a list but the check boxes should appear in horizontal pattern rather than the default vertical pattern. Is it possible using javascript?

  7. Boris Gomiunik
    February 18th, 2008 at 23:01
    Reply | Quote | #7

    Unfortunately this is a bit tougher, because all of those options are in a table and every option is in its own row. I’m thinking one option is to make a CSS hack to display table rows inline, or make paralel checkboxes and with onClick event you check or uncheck these options.

  8. Erik
    February 18th, 2008 at 23:19
    Reply | Quote | #8

    Thanks for the great tip on that explorer Boris.

    My problem is that the Date/Time fields are one field with a single Title but separate Id’s. I can modify the date part of the field no problem, it’s accessing the Hours/Minutes that is giving me grief. Everything I find will only work with Titles.

    Is there a way to set the value of the field based on the ID? I know SP adds the page information to the front of the field ID’s which makes it difficult to pick them out to set them.

    Thanks.

  9. Erik
    February 19th, 2008 at 19:19
    Reply | Quote | #9

    On the drive home I figured out how to access the time fields, The id on the Hour/Minute fields use the same ID as the date field but with hours and minutes added to the end, So I added two new vars:
    var myID_Hours
    var myID_Minutes

    Then added the following lines into your function above the return:
    myID_Hours = docTags[i].id+”Hours”;
    myID_Minutes = docTags[i].id+”Minutes”;

    This lets me reference the Hour/Minute field with getElementByID

    I hope this helps someone else. Drove me nutes. :D

  10. Boris Gomiunik
    February 20th, 2008 at 18:48

    Erik, that’s great. Would you mind if I’d make an extra post about this?

  11. Tanker
    February 20th, 2008 at 20:55

    Go ahead. If you want the whole code let me know and I can email it to you.

    Not sure you want it dumped here.
    I logged in with my Google Id this time so if you want to reach me you can through that.

    thanks

  12. Tim
    March 25th, 2008 at 16:37

    Hi,

    I posted the script snippet for the toggle option on the MSDN blog page but I’m short on time and have a system about to go live where Date Picker is being used for the start and end time fields in a list and users are complaining that they have to manually type the dates in.

    Can you please send me a copy of the routine you used, because I’m having trouble enabling it?

  13. Addis Abebayehu
    May 5th, 2010 at 18:37

    I was wandering if anybody had workd on or have an idea how this will be accomplished. I am adding a link in the toolbar of a dispform.aspx page. This link will opens up outlook and embed the dispform aspx fildes in the body of of the outlook. It will also pull the title name or id from the dispform.aspx page and add it to the subject field of outlook. How is this accomplished? Can I copy the dispform.aspx and paste it in outlook programatically? Please help.
    Thank you1