You need to sign in to do that
Don't have an account?
James L.
Can not get element ID by $Component in Javascript
In my code stuff below, ID mapped from $Component.xxx is always blank in Javascript.
I tried all the below options. none of them worked
alert('{!$Component.studentName}')
alert('"{!$Component.studentName}"')
however it's mapped correctly in Visualforce code stuff, like
<apex:outputLabel value="{!$Component.studentName}"/>
any idea how to use $Component.xxx correctly in Javascript?
here's the sample code
<apex:page controller="LearnWebService1"> <apex:form > <apex:pageblock > <apex:messages /> <apex:pageBlockSection columns="1"> <apex:pageBlockSectionItem > <apex:outputLabel value="{!$Component.studentName}"/> <apex:inputText id="studentName"/> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:pageBlockButtons > <apex:commandButton value="Create Through Ajax Call" onclick="alert('{!$Component.studentName}')"/> </apex:pageBlockButtons> </apex:pageblock> </apex:form> </apex:page>
Hi ,
you can get elements id in java script by using
function test(){
var a = document.getElementById('{!$component.formId.demo}');
alert(a);
}
<apex:page>
<apex:form id="formId">
<apex:pageblock >
<apex:outputLabel id="demo"/>
<apex:pageBlockButtons >
<apex:commandButton value="testjavascript" onclick="test()"/>
</apex:pageBlockButtons>
</apex:pageblock>
</apex:form>
</apex:page>
Thanks
Shailu
Appending all parents' id does work, like $Component.form.pageblock........myinput.
Just wonder if there's any other simple way, since it does work in Visualforce page like $Component.myinput
Found one explanation.
As long as the VF element is wrapped in either in "apex:form" or "apex:pageblock",
the $Component.xxx fails to work.
Here's a working example
Here's an example not working
If you use jQuery, you can use a selector matcher to get the ID:
this:
{!$Component.theForm.pgBlock.blockAgreementDate.x.contractDurationValue}
can be converted into
j$ = jQuery.noConflict();
j$('input[id$=contractDurationValue]')
Putting a $ after the id means the id must end in ...
---
Extract from http://stackoverflow.com/questions/1487792/jquery-find-element-whose-id-has-a-particular-pattern
There are 3 built-in attribute selectors for simple patterns:
That selector matches all spans that have an id attribute and it starts with foo (e.g. fooblah)
That selector matches all spans that have an id attribute and it ends with foo (e.g. blahfoo).
That selector matches all spans that have an id attribute and it has foo somewhere within in it (e.g.blahfooblah).