1,671 articles and 12,421 comments as of Saturday, August 21st, 2010

Monday, June 15, 2009

Quick and Easy: Use jQuery to Set A Text Field’s Value on a SharePoint Form

Note: Paul Galvin has graciously allowed us to cross post, all this week, to his series on jQuery. Thanks Paul.

I started playing around with jQuery yesterday.  I’ve been wanting to do this for a long time, ever since Paul Grenier started writing his series about jQuery for End Users at the venerable www.endusersharepoint.com web site.  As I use it, I hope to add a series of “Quick and Easy” posts like this one.  This post describes how to set a known text field’s value to anything you want.

In this scenario, I have created a custom list whose “new” form looks as shown:

Quick and Easy

This is the new form for a custom list with the default Title column and two list columns (not site columns; I don’t think it should make any difference).

The objective is to assign an arbitrary value to the field, “DefaultMeFieldNoSpaces” (you can tell I’m a bit of a coward with the “no spaces” thing going on, but I do spice it up at the end of this article).

This bit of jQuery worked for me:

As I understand it this bit of jQuery is saying, “find me any input tag whose title = DefaultMeFieldNoSpaces.  Then, set all of their values to a famous phrase from an old computer game.”

Since there will only be one field on the form with a title equal to “DefaultMeFieldNoSpaces” we are assured of assigning a value to that field and no other.

What about a field whose name has spaces in it?  It’s nearly the same:

I think this is a fairly safe approach, meaning that we should be able to find the field that we want and only the field we want.  If you look at the HTML SharePoint is giving us, it’s sort of messy:

“title” stands out as a recognizable and hopefully unique attribute to help us identify the specific column to which we want to assign our arbitrary value.
This is a foundational concept.  Setting a field in an arbitrary way like this isn’t going to win any awards.  However, if we want to do more interesting form level stuff (which all of us always want to do, of course, right after we finish washing the dishes), like change the value of “field b” automatically based on the value of “field a”, we (I) need to learn these things.

I think our best chance to get a real useful value here is via the title, at least for text fields.  There may be a better, more reliable approach.  If I find it, I’ll update this post.  If you know a better way, please leave a comment.

Paul Galvin, MVPPaul Galvin, Microsoft MVP – SharePoint
Web site: Paul Galvin’s SharePoint Space

Paul is a Solutions Architect currently working most closely with Microsoft Office SharePoint Server 2007. He was recently awarded Microsoft MVP – SharePoint status for his work with the SharePoint community.

View all entries in this series: PaulGalvin - Quick and Easy jQuery»
 

Please Join the Discussion

3 Responses to “Quick and Easy: Use jQuery to Set A Text Field’s Value on a SharePoint Form”
  1. Andy Burns says:

    I’ve done the same – Matching on “Title” seems to work pretty well for me too.

    One difference is that I use .val() rather than .attr E.g.

    $(’input[title=Assign Field With Space]‘).val(’You are in a twisty maze of passages, all alike.’);

    .val seems to take the difficulty out of dealing with different field types.

  2. ashoo says:

    Hi how do I get the value of a sharepoint field?

  3. Ryan says:

    Another example of this technique – also shows how you can get the javascript onto the editform.aspx without resorting to SharePoint Designer.

    Setting a default duration for new calendar events.
    http://blog.pentalogic.net/2009/09/setting-default-duration-for-new-calender-events/


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!