Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

html - How can I take text from <input> tag to javascript

I have made a program and it prompts the user to input text. Is there a way I can just take user input from the text box and then have the program take and use that text from there? Here's the code so far:

None of your corrections were working because I didn't tell you the what this program does. This program is supposed to take in a letter and then replace it with another letter encrypting the text, so I think that you need to see the entire code to figure this out;

function encodeFunction() {
  var text = prompt("Enter Text");
  document.getElementById("function").innerHTML = LetterChanges(text);
}

function decodeFunction() {
  var text = prompt("Enter Text");
  document.getElementById("function").innerHTML = LetterChanges(text, false);
}

function LetterChanges(str, encode = true) {
  let adjust = 1;
  if (!encode) {
    adjust = -1;
  }
}

}
  str = str.toLowerCase();
  let strArray = str.split("");
  let letterChange = strArray.map(function(value, index, array){
    if(str.charCodeAt(index) < 97 || str.charCodeAt(index) > 122){
      return value
    }else{
      return String.fromCharCode(str.charCodeAt(index)+adjust)
    }
  });
  
  return letterChange.join(""); 
}
<form>
  <input type="text" id="EString" name="EString"> <br><br>
</form>
<button onclick="encodeFunction()">Encode String</button>
<button onclick="decodeFunction()">Decode String</button>
<p id="function"></p>
question from:https://stackoverflow.com/questions/65858687/how-can-i-take-text-from-input-tag-to-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your encode and decode functions, you can change:

var text = prompt("Enter Text");

to

var text = document.getElementById("EString").value;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...