简体   繁体   中英

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>

In your encode and decode functions, you can change:

var text = prompt("Enter Text");

to

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

If you'd like to get the value of the text input(id='EString') you can use

document.getElementById('EString').value

You could simply get the value of document.getElementById("EString") , eg

 function encodeFunction() { var text = document.getElementById("EString").value; document.getElementById("function").innerHTML = LetterChanges(text); } function decodeFunction() { var text = document.getElementById("EString").value; document.getElementById("function").innerHTML = LetterChanges(text, false); } function LetterChanges(str, encode = true) { let adjust = 1; if (;encode) { adjust = -1; } return str; }
 <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>

 function myChange(){ console.log(document.getElementById("EString").value); }
 <form> <input type="text" onchange="myChange()" id="EString" name="EString" style=" width:1000px; height:500px;"></input> </form>

Check the below code for your desired result:

 function encodeFunction() { var text = document.getElementById("EString").value; document.getElementById("function").innerHTML = LetterChanges(text); } function decodeFunction() { var text = document.getElementById("EString").value; document.getElementById("function").innerHTML = LetterChanges(text, false); } function LetterChanges(str, encode=true) { let adjust = 1; if(;encode){ adjust = -1; } return adjust; }
 <form> <input type="text" id="EString" name="EString" style=" width:1000px; height:500px;" /> <br><br> </form> <button onclick="encodeFunction()">Encode String</button> <button onclick="decodeFunction()">Decode String</button> <p id="function"></p>

I made some changes in your code.

<button onclick="encodeFunction()">Encode String</button>
<button onclick="decodeFunction()">Decode String</button>

<p id="function"></p>


<script>
        function encodeFunction() {
  var text = document.getElementById("EString").value;
  document.getElementById("function").innerHTML = LetterChanges(text);
}

function decodeFunction() {
  var text = document.getElementById("EString").value;
  document.getElementById("function").innerHTML = LetterChanges(text, false);
}

function LetterChanges(str, encode=true) { 
  let adjust = 1;
  if(!encode){
    adjust = -1;
  }
return "Text recieved in JavaScript: " + str;
}
</script>

Use oninput and onchange attributes in input tag to call javascript function, because oninput is not working in some browsers.

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