javascript - Call JS function for specific id in HTML -


i have below function in js file name hello.js inside js folder.

js

function hello(){     alert('hello world !); } 

html

<!doctype html> <html>     <head>     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>         <script type="text/javascript" src="js/hello.js"></script>         <script>             $(function() {                 $("#hello").hello();             });         </script>     </head>     <body>         <button type="button" id="hello">click me!</button>      </body>     </html> 

how attach hello() function button id="hello"? i'm doing wrong can't find what.

edit : recommend reading answers completeness.

edit2: purpose of question clarify general method of attaching functions specific elements on html. button , click interaction example.

there many ways handle events html or dom.

defining in html

<button type="button" id="hello" onclick="hello();">click me!</button>  

using jquery

$("#hello").click(hello); 

attaching function event handler using javascript:

var el = document.getelementbyid("hello");     if (el.addeventlistener)         el.addeventlistener("click", hello, false);     else if (el.attachevent)         el.attachevent('onclick', hello);  function hello(){             alert("inside hello function"); } 

useful links

mdn - onclick event

so - ans 1

so - ans 2


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -