function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Avinash BarolaAvinash 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>
Abhishek_DEOAbhishek_DEO
There are 2 issues with your code.

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 
 
<apex:page standardController="Class__c">
    <script src="/soap/ajax/33.0/connection.js"></script>
    <script src="/soap/ajax/33.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.frm.block1.block2.name}').value;
                t.Name = nameOfClass;
                alert(nameOfClass);
                 var result = sforce.connection.create([t]);
                alert(result);
            }
               catch(error) {
                alert(error.faultstring);
            }
            
           };
    </script>
   
        <apex:form id="frm">
            <apex:pageBlock id="block1">
                <apex:pageBlockSection id="block2">
                    <apex:inputText id="name" />
                </apex:pageBlockSection>
                <apex:commandButton value="Save" onclick="save()" />
            </apex:pageBlock>
        </apex:form>
</apex:page>

Please mark this as an answer, if it helps you.