简体   繁体   中英

Creating objects and calling JavaScript methods from HTML

I am not very familiar with JavaScript and I'am a little confused with the object oriented peculiarities of it. I am trying to create a Caesar shift using the concepts of objects, methods and properties in JavaScript. I am using an HTML form to take input from user and OnClick return the encoded cipher text to user. This is what I have. I'm pretty much sure about my logic but I guess my object creation and method calls fall through. Am I doing it right? Any help is appreciated. Thanks!

    <head>
    <script>
    function Caesar(order){
    this.order = order;
    this.encode = encode;
    function encode(input){
    this.output = '';
    for (var i = 0; i < input.length; i++) {
    var this.c = input.charCodeAt(i);
    if      (this.c >= 65 && this.c <=  90) this.output += String.fromCharCode((this.c - 65 + this.order) % 26 + 65);  // Uppercase
    else if (this.c >= 97 && this.c <= 122) this.output += String.fromCharCode((this.c - 97 + this.key) % 26 + 97);  // Lowercase
    else   this.output += input.charAt(i);    
    }
    return answer.innerHTML= output;
     } 
    </script></head>
    <body>

    <form>
    Enter Plaintext : <input type = "text" name = "plaintext"> 
    Enter Shift:      <input type = "text" name = "shift"><br> --How do I get the input from here and create my caesar object?
    <input type="button" value="Encode" onclick ="encode()"> --How do I call the encode() method with input from the plaintext text box?  
    </form>
    </body></html>

Give the plaintext-textbox an id through which you can receive it's value in the JavaScript.

<input type="text" name="plaintext" id="plain_text">

In your JavaScript, get the HTML element by using document.getElementById(element_name) :

var plainTextBox = document.getElementById('plain_text');

Retrieving the value from the textbox is as easy as:

plainTextBox.value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM