0

I'm trying to make button (using bootstrap), which calls javascript function. I made this:

<head>
   <script type="text/javascript">
      function logInClicked() {
      var username = document.getElementById("inputUsername").innerHTML;
      var password = document.getElementById("inputPassword").innerHTML;
      alert(username + ":" + password);
      }
   </script>
</head>

<body>
    <form role="form">
        <div class="form-group" style="width:300px">
            <input type="email" class="form-control" id="inputUsername" placeholder="Username">
        </div>
        <div class="form-group" style="width:300px">
            <input type="password" class="form-control" id="inputPassword" placeholder="Password">
        </div>
    </form>

    <p>
        <button type="button" style="width:148px" class="btn btn-default btn-lg" id="signUpButton" onclick="signUp();">Sign Up</button>
        <button type="button" style="width:148px" class="btn btn-primary btn-lg" id="logInButton" onclick="logInClicked();">Log In</button>
    </p>
</body>

It shows button, but when I click, nothing happens. Please help.

1
  • logInClicked() is called. jsfiddle.net/h22eh Commented May 18, 2014 at 19:03

2 Answers 2

1

Assuming your inputUsername and inputPassword are input fields, try the following:

<body>
    <button type="button" style="width:148px" class="btn btn-primary btn-lg" id="logInButton">Log In</button>
<script type="text/javascript">
  function logInClicked() {
      var username = document.getElementById("inputUsername").value;
      var password = document.getElementById("inputPassword").value;
      alert(username + ":" + password);
  }
  document.getElementById('logInButton').addEventListener('click', logInClicked);
</script>
</body>

Functionality added via addEventListener instead of using the onclick attribute on the button.

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

1 Comment

Do you have the inputUsername and inputPassword inputs in your HTML? They are missing in the code you provided.
0

Input tags don't have innerHTML, they have value:

Demo

http://jsfiddle.net/nabil_kadimi/JgDQ6/

1 Comment

The problem is that function logInClicked() is not calling

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.