You need quotes around the DOM id value in the for
attribute selector. You also do not need the $
to apply 'ends with' selector matching, you already know the exact id value of the field being targeted and can use it directly.
You have this:
jQuery("label[for$=" + jQuery(field).attr('id') + "]")
instead you want do this (note the single quotes and the dropped $ modifier):
jQuery("label[for='" + jQuery(field).attr('id') + "']")
which would evaluate like this at runtime and match the label:
(presuming the label actually has a for
attribute on it with this value)
jQuery("label[for='j_id0:j_id106:j_id107:j_id108:j_id117:newContactForm:NewContactAccountSelect']")
Somewhat related, if you add ids to all of the <apex:
tags in your page, you can gain a slightly better looking element id hierarchy - if that is important to you. For example, if you give the <apex:page
tag an id="thePage"
, the first node in the element id value would be thePage
instead of j_id0
.
Also, another question: Is it possible to remove the partialId's added by salesforce and get the actual Id of form element by using javascript, jQuery etc..,?
There is no such thing as a partial ID. Element id values are supposed to be unique in the page. In order to accomplish this when VF renders HTML, the hierarchy of tags in the page are evaluated and the id value of the DOM element is constructed in a way that guarantees uniqueness.
It looks like you have learned about the 'ends with' $
selector modifier but if you want to output the a DOM id value using a VF-native feature, you can do so with the $Component
global function. This will render out the DOM ID of the component into the page.
For instance, if you were to add alert('{!$Component.NewContactAccountSelect}');
to your VF markup and you did it someplace where the renderer could find that component in the hierarchy, in your page source you would see alert('j_id0:j_id106:j_id107:j_id108:j_id117:newContactForm:NewContactAccountSelect');
and have an alert popped with that string value in it.
If $Component.NewContactAccountSelect
couldn't be found in the hierarchy, you'll just get an empty string there in the alert.
Documentation about best practices for referencing component ids in JavaScript: https://developer.salesforce.com/docs/atlas.en-us.salesforce1api.meta/salesforce1api/pages_best_practices_accessing_id.htm
Best Answer
You are closing the
span
tag two times:<span class="glyphicon glyphicon-search"/></span>
Just replace it with:
<span class="glyphicon glyphicon-search"></span>