0

As the title suggest, somehow document.getElementById doesn't seem to retrieve the elements in the line:document.getElementById("text").innerHTML = saltgen(document.getElementById("num"));

For context, I have unpacked the code into the Chrome extension. Here is Code:

document.getElementById("click").onclick = function() {myFunction()};
function myFunction() {
 document.getElementById("text").innerHTML = saltgen(document.getElementById("num"));
}
function saltgen(length) {
 var salt = "";
 var symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
 var slength = symbols.length;
 for (var i = 0; i < length; i++) {
 salt = salt + symbols.charAt(Math.floor(Math.random() * slength));
 }
 return salt;
}
<html>
<br>
<h1>Set the number of characters</h1>
<input id="num" type="number" value="20"><br><br>
<button id="click">Generate</button>
<p id="text">Salt goes here</p>
<script src="jquery-3.5.1.min.js"></script>
<script src="salt_genj.js"></script>
</html>

The error I used to have is "getElementById is not defined" at the line document.getElementById("text").innerHTML = saltgen(document.getElementById("num")); " but after I have reloaded the error somehow doesn't show up, but when the button is clicked, the text "Salt goes here" simply disappears instead of being replaced with the result of the function defined in the javascript file.

Randy Casburn
14.2k1 gold badge19 silver badges32 bronze badges
asked Feb 15, 2021 at 22:02
6
  • 5
    You are passing the retrieved element to saltgen() instead of the value: document.getElementById("num").value will return the value of the input. Also you should use textContent instead of innerHTML unless you specifically need the new value parsed as HTML. Commented Feb 15, 2021 at 22:05
  • @pilchard how would you pass the value into saltgen() istead of the element by itself? Commented Feb 15, 2021 at 22:09
  • 2
    As I noted above use document.getElementById("num").value Commented Feb 15, 2021 at 22:09
  • why doesn't not work, so it does work? Commented Feb 15, 2021 at 22:10
  • @pilchard Thanks a lot, it worked! Commented Feb 15, 2021 at 22:15

2 Answers 2

1

You are passing the retrieved element to saltgen() instead of the value.

You can retrieve the value from the element by accessing its value property: document.getElementById("num").value will return the value of the input.

Also, unless you specifically need the new value parsed as HTML, you should use textContent instead of innerHTML to set the new text.

document.getElementById("click").onclick = function() {myFunction()};
function myFunction() {
 document.getElementById("text").textContent = saltgen(document.getElementById("num").value); // Access 'value' property
}
function saltgen(length) {
 var salt = "";
 var symbols = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
 var slength = symbols.length;
 for (var i = 0; i < length; i++) {
 salt = salt + symbols.charAt(Math.floor(Math.random() * slength));
 }
 return salt;
}
<html>
<br>
<h1>Set the number of characters</h1>
<input id="num" type="number" value="20"><br><br>
<button id="click">Generate</button>
<p id="text">Salt goes here</p>
<script src="jquery-3.5.1.min.js"></script>
<script src="salt_genj.js"></script>
</html>

answered Feb 15, 2021 at 22:34
Sign up to request clarification or add additional context in comments.

Comments

0

You can use debugging, or console output, to see whats wrong

function myFunction(){ 
 let num = document.getElementById("num");
 let salt = saltgen(num);
 console.info("num", num, "salt", salt)
 document.getElementById("text").innerHTML = salt;
 }

'num' is not the number but the element

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.