+ Start a Discussion

How to bind the value in INputText using java script?


 I got one error in this program. please solve this problem.


My code is:


<apex:page id="thepage" sidebar="false" showHeader="false" >

<script language="javascript" type="text/javascript">
function randomString() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
document.getElementById('thepage.randform.randomfield').value = randomstring;

<apex:form id="randform">
<h1><b>Create Random Number</b></h1>
<apex:commandButton value="Create Random String" onClick="randomString()" />
<apex:inputText id="randomfield" value="{!randomvalue}"/>


You need the Visualforce global variable $Component in order to get the generated element's fully-qualified DOM Id for use in Javascript (or in plain HTML). Replace your line with this: 


document.getElementById('{!$Component.randform.randomfield}').value = randomstring;


Thank u Mike........