最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Call JS function for specific id in HTML - Stack Overflow

programmeradmin2浏览0评论

I have below function in JS file name as hello.js inside js folder.

JS

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

HTML

<!DOCTYPE HTML>
<html>
    <head>
    <script src=".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 do I attach the hello() function to the button with the id="hello"? I'm doing something wrong but I can't find what.

Edit : I remend reading all answers for pleteness.

Edit2: The purpose of this question was to clarify the general method of attaching functions to specific elements on html. The button and the click interaction was an example.

I have below function in JS file name as hello.js inside js folder.

JS

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

HTML

<!DOCTYPE HTML>
<html>
    <head>
    <script src="https://ajax.googleapis./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 do I attach the hello() function to the button with the id="hello"? I'm doing something wrong but I can't find what.

Edit : I remend reading all answers for pleteness.

Edit2: The purpose of this question was to clarify the general method of attaching functions to specific elements on html. The button and the click interaction was an example.

Share Improve this question edited Jul 23, 2015 at 12:23 hardstudent asked Jul 23, 2015 at 11:49 hardstudenthardstudent 631 gold badge1 silver badge8 bronze badges 2
  • api.jquery./on – Quentin Commented Jul 23, 2015 at 11:51
  • You are currently trying to call hello as a jQuery extension method. You could write it as a jQuery hello plugin, but just using click will do :) – iCollect.it Ltd Commented Jul 23, 2015 at 11:53
Add a ment  | 

4 Answers 4

Reset to default 4

There are many ways to handle events with HTML or DOM.

Defining it in HTML

<button type="button" id="hello" onclick="hello();">Click Me!</button> 

Using JQuery

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

Attaching a function to the 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

You are probably looking to bind click event on button with id hello using hello as handler

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

Use .on() to bind event handler.

$("#hello").on('click', hello);

Pure javascript:

var elm=document.getElementById("hello");
elm.onclick= function{ hello();};

Jquery:

$("#hello").click(hello() );
发布评论

评论列表(0)

  1. 暂无评论