-9

I have a js and htlm code in jfiddle.

I have created the button onclick event on the object method and on the page load it firing the event. Is this some problem with my code.

var user = {
  data: [
    {name: 'T. Woods', age: 37},
    {name: 'P. Mickelson', age: 43}
  ],
  clickHandler: function (event) {
    console.log(this);
    var randomNum = ((Math.random() * 2 | 0) + 1) - 1; // Random number between 0 and 1
    $('input').val(this.data[randomNum].name + ' ' + this.data[randomNum].age);
  }
};

console.log(user);
$('button').click(user.clickHandler(this));
<!DOCTYPE html>
<html>

<head>
  <title>Page Title</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>
  <h1>Homepage Headline</h1>
  <div id="div1">
    <p>
      <button>Get Random Person</button><br>
      <input type="text">
    </p>
  </div>
  <script type="text/javascript" src="oops.js"></script>
</body>

</html>
Shiny
  • 4,945
  • 3
  • 17
  • 33
devendra singh
  • 219
  • 3
  • 15

4 Answers4

4

Your click event listener needs to be a function otherwise it will trigger .click() on the button instead of creating a listener

See the documentation for .click()

$("button").click(function () {
   user.clickHandler(this)
});

Test it with your code here -

var user = {
 data :[
 {name: "T. Woods", age:37},
 {name: "P. Mickelson", age:43}
 ],
 clickHandler:function (event) {
  console.log(this);
 var randomNum = ((Math.random () * 2 | 0) + 1) - 1; // random number between 0 and 1
 //console.log(this.data[randomNum].name + " " + this.data[randomNum].age);
  // This line is adding a random person from the data array to the text field
  $ ("input").val (this.data[randomNum].name + " " + this.data[randomNum].age);
 }
};
console.log(user);
$("button").click(function() {
  user.clickHandler(this)
});
<!DOCTYPE html>
<html>
    <head>
        <title>Page Title</title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
     
    </head>
    <body>
        <h1>Homepage Headline</h1>
  <div id="div1">
    <p>           
        <button>Get Random Person</button><br>
         <input type="text">
      </p>
        </div>
        <script type="text/javascript" src="oops.js"></script>
    </body>
</html>
ButchMonkey
  • 1,873
  • 18
  • 30
  • thanks but my problem is click event firing on page load. Can you explain it more. – devendra singh Jan 21 '20 at 10:55
  • 2
    [`.click()`](https://api.jquery.com/click/) expects a callback function, but by adding `(this)` you're invoking the function immediately – Shiny Jan 21 '20 at 11:08
  • If this answer has helped you, please consider 'accepting' it; This will mark your question as solved, and also reward both you and the answerer some reputation - [How does accepting an answer work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – ButchMonkey Jan 22 '20 at 13:43
2

The issue is because you invoke the user.clickhandler() function when the page loads and set its response as the click handler for the element.

You instead want to provide the reference to the function to the event handler. You will also need to bind() the scope of user to the reference, as that's what clickhandler() expects to run under. Try this:

$("button").click(user.clickHandler.bind(user));

Working example:

var user = {
  data: [{
    name: "T. Woods",
    age: 37
  }, {
    name: "P. Mickelson",
    age: 43
  }],
  clickHandler: function(event) {
    console.log(this);
    var randomNum = ((Math.random() * 2 | 0) + 1) - 1; 
    $("input").val(this.data[randomNum].name + " " + this.data[randomNum].age);
  }
};
$("button").click(user.clickHandler.bind(user));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<h1>Homepage Headline</h1>
<div id="div1">
  <p>
    <button>Get Random Person</button><br>
    <input type="text">
  </p>
</div>
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
1

You need to attach the handler, not call it, and then bind the this object accordingly:

user.clickHandler.bind(user)
Romi Halasz
  • 1,949
  • 1
  • 13
  • 23
-1

This is not a problem, you call

$("button").click(user.clickHandler(this)); 

at the loading, then it call the click event

Replace it by

$("button").bind("click", user.clickHandler(this));