Solved

Use HTML Button OnClick to call a JavaScript function in another file

Posted on 2013-12-02
2
28,614 Views
Last Modified: 2013-12-02
How do I include an external JavaScript file so that a HTML Button OnClick has access to it?

Please provide an example.

Thank you
0
Comment
Question by:AmbassadorSpock
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 8

Accepted Solution

by:
Mohit Vijay earned 500 total points
ID: 39690750
assume you have a javascript file called myfile.js @ the root of application

so you have to give ref. of that fine in head tag of html

<script src="~/myfile.js"></script>

now in this myfile.js, you can write a function that will be called on click on html button. assume the function name is buttonClicked

so in js file, you can write

function buttonClicked()
{
alert('button click called');
}

And for html button you can write

onclick="javascript:buttonClicked();"
0
 

Author Closing Comment

by:AmbassadorSpock
ID: 39690797
Great....thank you
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question