-1

I'm trying to make a page that you type in something in the textbox and the page will alert the text in the textbox but look at the function alert it's not working please help!!!

<!DOCTYPE html>
<html>

<head>
  <script>
    function alert() {
      var userInput = document.getElementById("1").value;
      var email = document.getElementsByClassName("gb_b gb_7a gb_R gb_3a").innerHTML;
      window.alert("From: ", +email + " Message: " + userInput);
    }
  </script>
  <base target="_top">
</head>

<body>
  <input id="1" type="text" value="Write what you want the page to say">
  <button onclick="alert()">Enter</button>
</body>

</html>

4
  • Check console for errors.. Why function name is alert ? Commented Apr 29, 2016 at 6:28
  • where is the element with class "gb_b gb_7a gb_R gb_3a" in teh form? Commented Apr 29, 2016 at 6:30
  • @gurvinder372, That is a valid syntax! document.getElementsByClassName(names); Commented Apr 29, 2016 at 6:31
  • this is element is from google docs to get the users email. im trying to make an addon test project Commented Apr 29, 2016 at 6:47

2 Answers 2

2

Rename the custom function into alertFunction() since it is causing conflict with javascript built-in alert() function.

Try the following code:

<script>
    function alertFunction() {
      var userInput = document.getElementById("inputid1").value;
      var email = document.getElementsByClassName("gb_b gb_7a gb_R gb_3a").innerHTML;
      alert("From: " +email + " Message: " + userInput);
    }
</script>
<base target="_top">

<body>
  <input id="inputid1" type="text" value="Write what you want the page to say">
  <button onclick="alertFunction();">Enter</button>
</body>

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

Comments

1

You are calling alert() recursively without any exit condition, as window.alert refers to your implementation of alert() itself. If you look into your console's log you will see call stack exceeded error.

 function alert() {
     var userInput = document.getElementById("1").value;
     var email = document.getElementsByClassName("gb_b gb_7a gb_R gb_3a").innerHTML;
     alert("From: " + email + "Message: " +userInput); //This refers to your alert
}

You should rename the function as suggested in comments or if you want to override the default behaviour of the alert() function then do not call it from inside of your function.

function custom_alert() {
  var userInput = document.getElementById("1").value;
  var email = document.getElementsByClassName("gb_b gb_7a gb_R gb_3a").innerHTML;
  //window.alert("From: ", +email + " Message: " + userInput);
  console.log("From: "+email + " Message: " + userInput);

}

Comments

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.