0

I am using M.Qassas' "Show / Hide fields based on choice field selection using JQuery in SharePoint 2013" and I've stumbled across an issue. When my Titles have a Full Stop (.) in them the script will not work as wanted. I'll attach my code below. Has anyone else had this experience before and a work around for it?

<script type="text/javascript">
$(document).ready(function(){
jQuery('.ms-metadata').each(function(i,obj){var dest = jQuery(obj).parents('tr:first').find('td.ms-formlabel'); jQuery(dest).append('<br>');jQuery(obj).appendTo(dest);})
jQuery('.ms-formlabel').css({'white-space':'normal'});
//Show/hide columns based on Drop Down Selection
$("select[title='2.18 Private Sector Interest']").change(function() {
if ($("select[title='2.18 Private Sector Interest']").val() != "Yes")
{
$('nobr:contains("2.18a Details of Private Sector Interest")').closest('tr').hide();
}
else
{
$('nobr:contains("2.18a Details of Private Sector Interest")').closest('tr').show();
}
});
});
</script>
  • 1
    "." will be considered as a CSS class name in jQuery selectors so you might need to change them with empty string – Arsalan Adam Khatri Dec 14 '17 at 10:03
  • @ArsalanAdamKhatri Sorry for asking what is probably a very basic question but what do you mean by empty string? – Gareth James Dec 14 '17 at 10:22
  • As Danny answered, escape the notation will resolve the issue. – Arsalan Adam Khatri Dec 14 '17 at 11:58
2

Your jQuery selector doesn't work with CSS selector notation

$("select[title='218.Private.Sector.Interest']")

Fails

You can escape the notation:

$("select[title='218\\.Private\\.Sector\\.Interest']")

https://learn.jquery.com/using-jquery-core/faq/how-do-i-select-an-element-by-an-id-that-has-characters-used-in-css-notation/

or use partial matching:

$("select[title*='218'][title*='Private']")

https://api.jquery.com/category/selectors/attribute-selectors/


Note: You can show/hide/color Form fields without using jQuery, with vanilla JavaScript:

How to display and hide columns on Display form based on whether they have data or not

  • I'm not sure if I'm misunderstanding you but I've tried $("select[title='2\\.18 Private Sector Interest']") to no luck - I've updated the example above to the code that's currently not working. – Gareth James Dec 14 '17 at 13:46

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.