2

I want to output a function secret(); so it will look like this:

AAAA-BBB-<random string>

But it returns this error:

Uncaught TypeError: secret is not a function

Here is the code :

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Random End String</title>
  <script type="text/javascript">
    function writeName() {
      var welcomeMsg = document.getElementById('welcome');
      var name = document.getElementById('name');
      var alamat = document.getElementById('alamat');
      var secret = secret();
      var formContent = document.getElementById('entername');
      welcomeMsg.innerHTML = "Your Identifier is " + name.value + "-" + alamat.value + "-" + secret + "";
      formContent.innerHTML = "";
    }
  </script>
  <script>
    function secret() {
      var text = "";
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
      for (var i = 0; i < 5; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    }
  </script>
</head>
<body>
  <p id="welcome"></p>
  <form id="entername">
    Your Name:<input type="text" id="name" /><br/> LockerID:
    <input type="text" id="alamat" /><br/>
    <input type="hidden" id="secret">
    <input type="button" value="Submit" onclick="writeName();" />
  </form>
</body>
</html>

1
  • the output is undefined, why it doesn't work Commented Aug 2, 2018 at 18:45

2 Answers 2

2

Don't name the var secret change it to something else in this example secret1 works fine'.

EDIT: You also need to return your "secret text".

Full working example:

function writeName() {
  var welcomeMsg = document.getElementById('welcome');
  var name = document.getElementById('name');
  var alamat = document.getElementById('alamat');
  var secret1 = secret();
  var formContent = document.getElementById('entername');
  welcomeMsg.innerHTML = "Your Identifier is " + name.value + "-" + alamat.value + "-" + secret1 + "";
  formContent.innerHTML = "";
}

function secret() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  return text
}

function writeName1() {
  var welcomeMsg = document.getElementById('welcome');
  var name = document.getElementById('name');
  var alamat = document.getElementById('alamat');
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

  for (var i = 0; i < 5; i++) {
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  }
  var formContent = document.getElementById('entername');
  welcomeMsg.innerHTML = "Your Identifier is " + name.value + "-" + alamat.value + "-" + text + "";
  formContent.innerHTML = "";
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
  <title>Random End String</title>
</head>

<body>
  <p id="welcome"></p>
  <form id="entername">
    Your Name:<input type="text" id="name" /><br/> LockerID:
    <input type="text" id="alamat" /><br/>
    <input type="hidden" id="secret">
    <input type="button" value="Submit" onclick="writeName();" />
  </form>
</body>

</html>

Your Name:<input type="text" id="name1" /><br/> LockerID:
<input type="text" id="alama1t" /><br/>
<input type="hidden" id="secret1">
<input type="button" value="Submit" onclick="writeName1();" />

Sign up to request clarification or add additional context in comments.

5 Comments

why it's still undefined?
Never mind check the edit you also need to return your "secret text"
Try it now it will return the secret text if you put nothing in the boxes or if you do it will return box1 text + box2 text + secret text
Cool, it's work, Many thanks for you, so for any function i should return the value right? i just learn javascript, this is so cool. but i'm curious, if javascript clientside, how do i hide the function for public like php? btw many thanks again.
You could just put the secret code in the same function and not call secret like the second example but if you want a function to return a value to you you have to return it
1

Your local variable secret is hiding your function secret. Simply rename the variable to something else to fix this issue

function writeName(){
 var welcomeMsg = document.getElementById('welcome');
 var name = document.getElementById('name');
 var alamat = document.getElementById('alamat');
 var secret = secret(); //right here this is your issue
 var formContent = document.getElementById('entername');

 welcomeMsg.innerHTML = "Your Identifier is "+name.value+"-"+alamat.value+"-"+secret+""; 
 formContent.innerHTML = "";

 }

1 Comment

@artasena I didn't correct your code but I added a comment indicating where your problem is

Your Answer

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.