1

I have a list with columns A, B, C, AA, BB, and CC.

I tried using the following code to hide column "B", but, as I expected, it hid both "B" and "BB".

<script src="http://code.jquery.com/jquery-1.7.2.min.js" type=text/javascript></script>
<script type="text/javascript">
$(document).ready(function(){
$('nobr:contains("B")').closest('tr').hide();
});
</script>

Then I tried the code below. I used the internal column name, which happened to be "B" as well. It also hid "BB" because "BB"'s internal column name was "BB" and it seems to be searching 'contains' rather than 'equals'.

<script src="http://code.jquery.com/jquery-1.7.2.min.js" type=text/javascript></script>
<script type="text/javascript">
$(document).ready(function(){
$('[id^="B"]').closest('tr').hide();
});
</script>

How do I get it to find only "B"? I need it to search for the exact internal column name.

9
  • Did "=" not work? "^=" means "begins with". Commented May 9, 2019 at 17:40
  • Thank for clarifying what "^=" means. I wasn't able to figure that out! :) I tried "=", but it does nothing at all, so I figured it wasn't a valid operator...? I tried "==" too. That doesn't work either. Commented May 9, 2019 at 17:45
  • Have you looked at the source of the page to see what the "id" attribute contains? (F12 tools, etc.) Commented May 9, 2019 at 17:46
  • Which page? Are you suggesting that "id" is not the field I think it is? Commented May 9, 2019 at 17:57
  • To clarify, I don't really NEED to use "id". I'm find with looking up the column name instead like in the first snippet, because I'll never have two columns with the same name. But I'm not sure how I can do that. Cleary ".contains()" will not work, but I don't know what the alternative would be. Commented May 9, 2019 at 18:04

2 Answers 2

1

You could just add some extra logic to make sure you're affecting the row for the exact label you are looking for:

$('nobr:contains("B")').each(function() {
    // the BB column will not meet this condition
    if ($(this).text() === "B") {
        $(this).closest('tr').hide();
    }
});

So, just to be clear on the two approaches, when you are looking for the nobr:contains with the display name of the column, that is looking at the column labels that are on the left hand side of the form. SharePoint wraps the display names of the columns in a nobr tag, so that's why that works. On the other hand, when you are using the [id^="FieldInternalName"] way of finding the row you want, you are looking for the control for that column that is on the right hand side of the form (i.e., the textbox, the people picker, the drop-down, etc.). The reason Ganesh's suggestion won't work, and the other answer you found uses the "starts with" (^=) comparison is because when building the control for that field, SharePoint creates the ID this way:

id="FieldInternalName_some-guid-here_TypeOfControl"

so the ID of the control is more than just exactly the internal name, and you have to use "starts with" to find it.

In your case, [id^="B"] will also find your BB column, because of course BB starts with B. However, if you take into account the fact that after the internal column name comes an underscore and then a GUID, this might work for you: [id^="B_"].

Keep in mind, though (and this is to somewhat address your comment that the internal field names are "automatically assigned by SharePoint") - when you create a column, SharePoint assigns the internal name of the column based on what you name the column, and people quite often name a column using more than one word, so there are spaces in there. This causes SharePoint to encode the spaces as _x0020_ in the internal name.

You can control this.

If you create a column and do not use spaces or any other special characters (like apostrophes or parentheses, they get encoded too, just with a different code than spaces) when you initially create the column, the internal name will be nice and neat. You can then go back and edit the column and make the display name whatever you want, the internal name will already be set. So for instance, if you wanted to create a site column "Phone Number", if you name it like that initially, the internal name will be Phone_x0020_Number, which is not great. Instead, you should name the column "PhoneNumber" initially, which will give it an internal name of PhoneNumber, and then you go back and add the space and make the display name "Phone Number", but the internal name will stay PhoneNumber.

Where am I going with all this?

Well, not controlling the internal names of the columns is one of my biggest peeves with how people use SharePoint, so I tend to get a bit ranty about it. BUT, I will bring it back around to tie it in to this question and answer.

Let's say that someone had created a column on your site or list or wherever that was called "B and B", and they hadn't used the best practice method of controlling the internal name by not using spaces. If they used spaces, the internal name for that column would be B_x0020_and_x0020_B. So if that were the case, using [id^="B_"] would not work for distinguishing between your B column and the B and B column.

So that's just something you have to keep in mind and watch out for.

3
  • Awesome! This fixed it! Thank you for the detailed explanation. I might go back and redo all my columns that have spaces because that is super annoying. For anyone else with this problem, here's the full snippet I used: <script src="http://code.jquery.com/jquery-1.7.2.min.js" type=text/javascript></script> <script type="text/javascript"> $(document).ready(function(){ $('nobr:contains("B")').each(function() { if ($(this).text() === "B") { $(this).closest('tr').hide(); } }); }); </script> Commented May 9, 2019 at 21:14
  • Glad it helped, but keep in mind that if you go back and "redo" all your columns, you will technically be creating brand new columns. When you create the columns, SharePoint assigns it an internal name and an ID, and those can't be changed. So you would have to create the new columns with the controlled internal names, but then you wold have to copy any data in the old columns they're meant to replace into the new columns. Commented May 9, 2019 at 21:29
  • Got it. My lists don't have data yet, so it's not a big deal if I have to recreate the columns. Commented May 9, 2019 at 21:32
0

If you are not able to get the input field using starts with(^=) then try giving full id value instead of ^=.

Assume if you have field like:

<input id='B12edsw23f3d" />`

then use the selector as:

$("#B12edsw23f3d").closest('tr').hide();
2
  • But where is the input=id line? According to responses on other questions, "id" is supposedly the internal column id for columns in a list. Those are automatically assinged by SharePoint and you can find them at the end of the URL when you edit the column. For example, my "B" column has "Field=B" at the end fo the URL, indicating that "B"'s internal column id is "B". This is what I'm trying to search for. Commented May 9, 2019 at 18:46
  • On your form, you can inspect the input element and see the ID of them. Use those id in your code. Commented May 10, 2019 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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