1

I am a beginner at javascript and i am stuck with this piece of code. Syntactically the code seems to have no error but when it is executed there is no output, both in the webpage and the console. Hence I am confused and am unable to understand where the error lies.

function SubMit() {
  var userName = document.getElementById("user").value;
  document.getElementById("out").innerHTML = userName;

  var fName = document.getElementById("firstName").value;

  var lName = document.getElementById("lastName").value;

  var eMail = document.getElementById("email").value;

  var p1 = document.getElementById("pass1").value;

  var p2 = document.getElementById("pass2").value;

  console.log(userName);
  console.log(fName);
  console.log(lName);
  console.log(eMail);
  console.log(p1);
  console.log(p2);
}

function click() {
  document.getElementById("c").innerHTML = "HELLO WORLD";
  console.log("hello world");
}
<form>
  <input type="text" placeholder="Username" id="user" required>
  <input type="text" placeholder="First Name" id="firstName" required>
  <input type="text" placeholder="Last Name" id="lastName">
  <input type="email" placeholder="Email" id="email" required>
  <input type="password" placeholder="Password" id="pass1" required>
  <input type="password" placeholder="Confirm Password" id="pass2" required>
  <button type="submit" onclick="SubMit()">SUBMIT</button>
</form>
<output id="out"></output>

<button type="button" onclick="click()">click me</button>
<p id="c"></p>

1
  • The problem with the c button is most likely the name of the function, just rename it from click to something like btnClick. Commented Aug 2, 2020 at 17:34

2 Answers 2

1

click() is already defined as a javascript method

function SubMit() {
  var userName = document.getElementById("user").value;
  document.getElementById("out").innerHTML = userName;

  var fName = document.getElementById("firstName").value;

  var lName = document.getElementById("lastName").value;

  var eMail = document.getElementById("email").value;

  var p1 = document.getElementById("pass1").value;

  var p2 = document.getElementById("pass2").value;

  console.log(userName);
  console.log(fName);
  console.log(lName);
  console.log(eMail);
  console.log(p1);
  console.log(p2);
}

function myClick() {
  document.getElementById("c").innerHTML = "HELLO WORLD";
  console.log("hello world");
}
<form>
  <input type="text" placeholder="Username" id="user" required>
  <input type="text" placeholder="First Name" id="firstName" required>
  <input type="text" placeholder="Last Name" id="lastName">
  <input type="email" placeholder="Email" id="email" required>
  <input type="password" placeholder="Password" id="pass1" required>
  <input type="password" placeholder="Confirm Password" id="pass2" required>
  <button type="submit" onclick="SubMit()">SUBMIT</button>
</form>
<output id="out"></output>

<button type="button" onclick="myClick()">click me</button>
<p id="c"></p>

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

1 Comment

Might be good to explain where it's defined, e.g., in onclick you're already scoped to the element, and HTMLElements have a click method.
1

Change output tag with div tag as.

<div id="out"></div>

rename click() to any other name. click() is predefined js function and you have to override the function before using it preventing the click event.

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.