Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between alert("Hi!") and function(){alert("Hi!")} [duplicate]

Tags:

javascript

dom

When we are doing inline command in the button:

<button id="myButton" onclick="alert('Hi!')"> 

Why does

document.getElementById("myButton").onclick = alert('Hi!') 

not work but give the alert as the page is loaded? I can't understand how it works with function() added to it and without function(). I hope you guys understand my question. I'm missing something here.

like image 606
Yogi Avatar asked Jan 20 '16 06:01

Yogi


People also ask

What is difference between alert and window alert?

Difference between alert() and window. alert()window in JS is a global object, that means you can access it anywhere throughout your code. So, alert() becomes a global method / function. Therefore, you can call alert function of window object as window.

What is the difference between alert and return?

The only difference is that one has the alert as part of the function and the other does not. When run, your first function returns a value, which you then alert the user to independent of the function, whereas the other alerts the user to your result as part of the function.

Is alert still used in JavaScript?

One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen.


4 Answers

document.getElementById("myButton").onclick = alert('Hi!')

is wrong since onclick should be assigned to a function reference, not the function call result itself.

It will execute alert('Hi!') when the page is loaded but that is not the intention here, is it? The intention behind assigning an onclick handler is to ensure that when the button is clicked this alert will be executed.

For that to happen it should be:

document.getElementById("myButton").onclick = function(){alert('Hi!')};

Also, this will not work unless it is wrapped inside the window.onload event:

window.onload = function(){
    document.getElementById("myButton").onclick = function(){alert('Hi!')};
};
like image 95
gurvinder372 Avatar answered Oct 21 '22 23:10

gurvinder372


alert('Hi!') is a function call that alerts 'Hi' and returns nothing (undefined).

onclick expects to get a function and you are passing the function call's result which is undefined.

Since JavaScript is not a strong typed framework you don't get an error on bad assignments.

So why does the following work:

<button id = "myButton" onclick="alert('Hi!')">

It's because the html parser (in this case, your browser) does some work behind the scenes and wraps the call with a function.

like image 36
Amir Popovich Avatar answered Oct 22 '22 00:10

Amir Popovich


document.getElementById("myButton").onclick expects a function to be called later.

<button id = "myButton "onclick="alert('Hi!')"> expects a block of code to be executed later.

like image 7
Charlie Avatar answered Oct 21 '22 23:10

Charlie


alert('Hi') 

Here alert is an inbuilt function called by browser which opens a alert box.

function callAlert(){
  alert('Hi');
}

Here callAlert is a custom function which calls the inbuilt function alert

In your example, when appending a click event, you have to define the function

document.getElementById("myButton").onclick = alert('Hi!') //alert is 

already executed

document.getElementById("myButton").onclick = function (){ alert('Hi!') }; 
//a function is defined/declared which will be executed when the onclick action is performed
like image 6
madhairsilence Avatar answered Oct 21 '22 23:10

madhairsilence