0

I'm trying to get textbox inputs in my JS function on the click of a button. For some reason my console.log does not show the textbox inputs. How do I accomplish this?

Index.php:

  <input placeholder="Name" type='text' name='username' id='username'  maxlength="50" />
  <input placeholder="Password" type='password' name='password' id='password' maxlength="50" />
  <button id="testAJAX" onclick="Utilities.loadSavedGames()">Load Game</button>

js/utilties.js

var Utilities = {
    loadSavedGames : function () {

    var username = document.getElementById('username').value,
        password = document.getElementById('password').value;
    console.log(username + ", " + password); //null, blank
6
  • console.log in code or log Commented Jun 4, 2015 at 5:12
  • 1
    Works fine here Commented Jun 4, 2015 at 5:13
  • It looks OK. Can you post the full text of php and js file somewhere, so we can find a problem in another part. Commented Jun 4, 2015 at 5:13
  • @Tushar sorry that was a typo Commented Jun 4, 2015 at 5:15
  • 1
    @Growler, kindly make sure that same id is not being used on the same page. Try this code in the browser console: $('[id=username]').length and $('[id=password]').length, and let me know Commented Jun 4, 2015 at 6:32

2 Answers 2

1

Possibly you have elements with duplicate ids on the same page. Try this code in the browser console: $('[id=username]').length and $('[id=password]').length. If the count is > 1, you have multiple elements with same id.

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

Comments

0

Put console.log instead of log and also make utilities as global object. I mean remove var.

   Utilities = {
    loadSavedGames : function () {

       var username = document.getElementById('username').value,
        password = document.getElementById('password').value;
       console.log(username + ", " + password); 
      }
     }

2 Comments

Utilities should be declared. Isn't it?
I do not see how this solves the problem. Furthermore, removing the var from Utilities will cause a run-time error in at least some environments. Suggest deleting this answer.

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.