www.webdeveloper.com
Results 1 to 3 of 3

Thread: Convert String to ASCII Code?

  1. #1
    Join Date
    Apr 2011
    Posts
    3

    Question Convert String to ASCII Code?

    I'm trying to conver the String "Hopper" to ASCII code and this is what I have so far. Unfornately, it only displays the ASCII code for the last character or 114. And I would like to the code to display all the codes for every character. What am I doing wrong?

    Code:
    <script type="text/javascript">
    function thisFunction() {
    	var someString = "Hopper";
    	var j;
    	for(j = 0; j < someString.length; j++) {
    		document.thisForm.clearText.value =  someString.charCodeAt(j);
    	}
    }
    </script>
    
    <form name="thisForm">
    <input type="button" value="Pass Value" onclick="thisFunction()"/>
    <br/>
    <textarea rows="6" cols="30" name="clearText"></textarea>
    </form>

  2. #2
    Join Date
    Apr 2003
    Location
    Netherlands
    Posts
    21,654
    Concatanate
    Code:
    document.thisForm.clearText.value +=  someString.charCodeAt(j);
    At least 98% of internet users' DNA is identical to that of chimpanzees

  3. #3
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    It's because you are setting the value of the form field as just that one character as you loop the length of the string, so the last letter is the only ASCII code that will appear.

    Try this:
    Code:
    document.thisForm.clearText.value +=  someString.charCodeAt(j);
    This is shorthand for "field value equals itself plus additional data".

    (Och! Fang beat me to it!)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles