View Full Version : Help pls. new to Js

04-10-2005, 01:27 PM
hi all, i am new to js and need some help badly. i have managed to make a page for conversion to hex characters but the conversion is done only after user clicks on submit button i need it to be done as the user types. thanks in advance.

i am attaching my code if u copy and run the page in broser u can understand me better.

<HEAD><TITLE>Unicode 2 converter</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
.titre1 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
color: #225E7D;
font-style: italic;
var submitcount;
function reset()

function textCounter(field, countfield) {
countfield.value = field.value.length;
function textCountDown(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
countfield.value = maxlimit - field.value.length;

<SCRIPT language=javascript type=text/javascript>
//Fonction de formatage
function formatage (myvar, longueur)
while (myvar.length < longueur)
myvar = "0"+myvar
return myvar;

function encode(){
if(document.forms[0].ascii.value!= ''){
var vText = document.forms[0].ascii.value;
document.forms[0].hex.value = convertToHex(vText);
function convertToHex(num) {
var hex = '';
for (i=0;i<num.length;i++)
hex += formatage("0" + num.charCodeAt(i).toString(16).toUpperCase(),4);
return hex;


<META content="Microsoft FrontPage 4.0" name=GENERATOR></HEAD>
<FORM name="blah" action="">
<textarea class="titre1" onkeydown=textCounter(this.form.ascii,this.form.remLen); onkeyup=textCounter(this.form.ascii,this.form.remLen); name="ascii" cols="75" rows="1"></textarea>
&nbsp; <BR>
<INPUT tabIndex=2 readOnly maxLength=3 size=5 value=0 name="remLen">
<input name="hex" type="text" value="" size="75">
&nbsp; <BR>
<input name="reset" type="reset" value=Clear>
<INPUT name="button" type=button onclick=encode() value=Submit>

04-10-2005, 08:15 PM
ok to start off is there a reason why you use the following in the encode() function?


if you get rid of those and add the encode() function to the onkeyup event of the textarea, that should give you the result you want. the thing is if you do that if the user decides to delete all the content it will delete all the hex values except the very first value inputed because you have your script saying as long as the text area isnt ='' then do it. so i also recomend getting rid of that as well.