1,804 articles and 14,849 comments as of Wednesday, May 18th, 2011

EndUserSharePoint has combined resources with NothingButSharePoint.com. You can now find End User (Mark Miller), Developer (Jeremy Thake) and IT Pro SharePoint content all in one place!

This site is a historical archive and is no longer being updated. Please update your favorites, bookmarks and RSS feeds.

NothingButSharePoint.com
Thursday, June 18, 2009

Quick and Easy: A Better Way to Use jQuery to Hide a Text Field on a SharePoint Form

This is another post in Paul Galvin’s on-going series on how to use jQuery with SharePoint. If you want to learn more about jQuery, I highly recommend: jQuery in Actionhttp://www.assoc-amazon.com/e/ir?t=httppaulgalvi-20&l=ur2&o=1 by Bear Bibeault and Yehuda Katz.

Previously, I wrote about how to use jQuery to locate and hide a text field on a form.  I didn’t care for the specific approach (I was chaining parents – that’s simply isn’t done these days, at least in families of quality). 

When I first started to think about it, I knew I needed to find a <TR> to which I could invoke the hide() method.  My early effort to find the correct <TR> was something like this:

$('tr:has(input[title=Hide Me!])');

The problem with that is that it would find every <TR> tag that had any parent relationship to the Hide Me! field, even if Hide Me! is nested many levels deep in <TR>’s.  It turns out that on my sandbox form, that expression finds 9 different TR’s who have Hide Me! as a child somewhere in its DOM tree.  I realized that I could walk back up the tree from the input field itself, so that’s how I ended up abusing parents, but it didn’t sit well with me.

I gave some thought to this and one of the things I read finally made sense: I could use the not() method to trim out <TR>’s I don’t want in my wrapped set.  That led me to this:

$('tr:has(input[title=Hide Me!])').not('tr:has(tr)').hide();

The first bit finds all the <TR> tags that have the Hide Me! field anywhere in their own hierarchy.  It then strips out any <TR> that also have a child <TR>.  This leaves us with a single <TR> that:

1) Has no <TR> child records
2) Does have the input field as child. 

We can then apply the hide() method to the resulting set and we’re done.

I’m still a bit nervous about this, but not as nervous as chaining parents.

I don’t know if this is a best practice or not.  There may be a more appropriate way of identifying just the <TR> that we care about in a SharePoint form.  If you know, please post 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

10 Responses to “Quick and Easy: A Better Way to Use jQuery to Hide a Text Field on a SharePoint Form”
  1. Nathan Ahlstrom says:

    I’ve been using basically this:

    $(’nobr:contains(”Hide Me!”)’).closest(tr).hide();

    Enjoy,
    Nathan

  2. Erich O'Donnell says:

    Where do you drop this piece of code to get things to work?

  3. jason says:

    great article. I have iPhone app that pulls contact web parts in and formats them to look good. It only shows last name since that is primary key field, so I might see several ‘Jones’ on my phone. Is there way in jQuery to concatenate Last/First Name at runtime and show in list so I see whole name?

  4. Arkadiy says:

    I have tried to use all 3 methods on my NewItem page, but it only works for text field.
    How do I select DropDown? I noticed that SharePoint adds some text to Title, for example for my “Status” field SharePoint lists “Status: Specify your own value:” as title. Plugging that in the code didnt really work…Any ideas?

  5. Courtenay says:

    @Arkadiy:

    Try replace ‘input’ with ’select’

  6. Courtenay says:

    To hide a datepicker field:

    $(’tr:has(input[title=PAAdminStartDate])’).not(’tr:has(tr)’).parents(’tr:first’).hide();

  7. Priyanka says:

    Where do you drop this piece of code to get things to work?

  8. Priyanka says:

    Any ideas for dropdown content type?
    thanks

Trackbacks

Check out what others are saying about this post...
  1. [...] (already!): I did think of a better way to locate the <TR> tag I want to hide and wrote about it here.  You may still find this article interesting anyway so I’m leavnig it [...]




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!