...

View Full Version : Help pls. new to Js



designer_bhutan
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">
<style>
.titre1 {
font-family: Geneva, Arial, Helvetica, sans-serif;
font-size: 18px;
font-weight: bold;
color: #225E7D;
font-style: italic;
}
</style>
<SCRIPT>
var submitcount;
function reset()
{
submitcount=0;
}

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);
else
countfield.value = maxlimit - field.value.length;

}
</SCRIPT>
<SCRIPT language=javascript type=text/javascript>
<!--
//Fonction de formatage
function formatage (myvar, longueur)
{
myvar=String(myvar);
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);
document.forms[0].ascii.focus();
document.forms[0].ascii.blur();
document.forms[0].ascii.select();
}
}
function convertToHex(num) {
var hex = '';
for (i=0;i<num.length;i++)
hex += formatage("0" + num.charCodeAt(i).toString(16).toUpperCase(),4);
return hex;

}

</SCRIPT>
<META content="Microsoft FrontPage 4.0" name=GENERATOR></HEAD>
<BODY>
<P>&nbsp;</P>
<FORM name="blah" action="">
<B>Text</B><BR>
<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">
<BR>
<input name="hex" type="text" value="" size="75">
&nbsp; <BR>
<input name="reset" type="reset" value=Clear>
&nbsp;&nbsp;&nbsp;
<INPUT name="button" type=button onclick=encode() value=Submit>
</FORM>
</BODY>

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

document.forms[0].ascii.focus();
document.forms[0].ascii.blur();
document.forms[0].ascii.select();

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum