You need to sign in to do that
Don't have an account?
Avinash Barola
var nameOfClass = document.getElementById('{!$Component.block1.block2.name}').value; not working
<apex:page standardController="Class__c">
<script src="/soap/ajax/11.0/connection.js"></script>
<script src="/soap/ajax/11.0/apex.js"></script>
<script type = "text/javascript">
function save(){
try{
sforce.connection.sessionId = "{!$Api.Session_ID}";
var t = new sforce.SObject("Class__c");
var nameOfClass = document.getElementById('{!$Component.block1.block2.name}').value;
t.Name = nameOfClass.value;
alert(nameOfClass.value);
var result = sforce.connection.create([t]);
alert(result);
}
catch(error) {
alert(error.faultstring);
}
};
</script>
<apex:form>
<apex:pageBlock id="block1">
<apex:pageBlockSection id="block2">
<apex:inputText id="name" />
</apex:pageBlockSection>
<apex:commandButton onclick="save()" />
</apex:pageBlock>
</apex:form>
</apex:page>
<script src="/soap/ajax/11.0/connection.js"></script>
<script src="/soap/ajax/11.0/apex.js"></script>
<script type = "text/javascript">
function save(){
try{
sforce.connection.sessionId = "{!$Api.Session_ID}";
var t = new sforce.SObject("Class__c");
var nameOfClass = document.getElementById('{!$Component.block1.block2.name}').value;
t.Name = nameOfClass.value;
alert(nameOfClass.value);
var result = sforce.connection.create([t]);
alert(result);
}
catch(error) {
alert(error.faultstring);
}
};
</script>
<apex:form>
<apex:pageBlock id="block1">
<apex:pageBlockSection id="block2">
<apex:inputText id="name" />
</apex:pageBlockSection>
<apex:commandButton onclick="save()" />
</apex:pageBlock>
</apex:form>
</apex:page>
1. Id is missing from <apex:form> attribute // you should assign some value to ID property of apex:form attribute and use that in $component
for eg. {!$Component.myfrm.block1.block2.name} // myfrm is ID of form
2. to access the value, you just need to alert variable "nameOfClass" name not nameOfClass.value, because you are accessing value using document.getElementById().value
A working code would look like
Please mark this as an answer, if it helps you.