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
dai tran 6dai tran 6 

How can set value to apex:inputText from javascript?

<apex:form>
                    <apex:inputText id="txtName" value="{!name}" label="Input"/>
  </apex:form>
<script>
  var txtName = document.getElementById('txtName');
  txtName.value='abc';
</script>
Occur error: Uncaught TypeError: Cannot set property 'value' of null
How can set value to apex:inputText from javascript?
Best Answer chosen by dai tran 6
Sohan Raj GuptaSohan Raj Gupta
Hi Dai,

You can use jQuery for this. In this case your code look like below:
 
<apex:form>
                    <apex:inputText id="txtName" value="{!name}" label="Input" styleClass="txtName"/>
  </apex:form>
 
<script>
  jQuery(document).ready(function(){
  	jQuery('.txtName').val('abc');
  });
</script>

Before using jQuery, you need to add jQuery.

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 

All Answers

Sohan Raj GuptaSohan Raj Gupta
Hi Dai,

You can use jQuery for this. In this case your code look like below:
 
<apex:form>
                    <apex:inputText id="txtName" value="{!name}" label="Input" styleClass="txtName"/>
  </apex:form>
 
<script>
  jQuery(document).ready(function(){
  	jQuery('.txtName').val('abc');
  });
</script>

Before using jQuery, you need to add jQuery.

Hope this will help you. Let me know if it helped or you need any more assistance. 

Please mark this is as the solution if it solved your purpose.

Thanks,
Sohan Raj Gupta 
This was selected as the best answer
dai tran 6dai tran 6
Id of apex:inputtext had changed: <input id="j_id0:j_id4:txtName" type="text" name="j_id0:j_id4:txtName">
Sohan Raj GuptaSohan Raj Gupta
Hi Dai,

Your field ID will be changed on the basis of ORG. So you should class name instead of field ID.

Regards,
Sohan Raj Gupta
mukesh guptamukesh gupta
Hi,

you need to add  class attribute to access input text   
<apex:inputText class="cls"/>

 , because id will be change on run time in browser. So you can use class.



 if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
dai tran 6dai tran 6
@Mukesh: it occur error: Unsupported attribute class in <apex:inputText>
Srujan SSrujan S
@dai train 6 Use we need to use "StyleClass" for apex components