1

I am a bit of a newbie in JS and not too sure what I'm doing.

I have created a button

<div id="btn">
    <a href='dg-quiz-maker.js' class='button'>ATTEMPT A QUIZ</a>
</div>

I want when the user presses the button, the JS dg-quiz-makes.js file appears. I'm not sure how to I do it. Please help.

Rodrigo Taboada
  • 2,727
  • 4
  • 24
  • 27
user3438350
  • 11
  • 1
  • 6

5 Answers5

2

Steps to add a event :

1).Add selector (either class or id) to the element (ex: <--button class="classname">test<--/button-->)

2).In document ready - Add on click event and write the related code inside it.

Note : Add Jquery.js in HTML page

 $( document ).ready(function() {
    $('.classname').on('click',function(){
    /** write related code inside **/
      yourfunc();
    });
 });

function yourfunc(){
alert('this is the other function');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="classname">test</button>
Rajesh Grandhi
  • 378
  • 3
  • 13
0

What you can do is make a javascript function and call it on click like this:

<div id="btn">
   <a href='#' onclick="myFunc();" class='button'>ATTEMPT A QUIZ</a>
</div>

<script>
function myFunc(){
   alert('test');
}
</script>

Or you can include functions from js files like this:

<script src="dg-quiz-maker.js"></script>
Muhammad Bilal
  • 2,106
  • 1
  • 15
  • 24
0

you just cant use .click() as some browser wont pick it up. please test after using .

   <script type="text/javascript">
function performClick(elemId) {
   var elem = document.getElementById(elemId);
   if(elem && document.createEvent) {
      var evt = document.createEvent("MouseEvents");
      evt.initEvent("click", true, false);
      elem.dispatchEvent(evt);
   }
}
</script>
<a href="#" onclick="performClick('theFile');">Open file dialog</a>
<input type="file" id="theFile" />
Waqar ul islam
  • 418
  • 4
  • 17
  • Thanks, but that doesn't do what I want it, it just creates a Choose File button next to mine. I want JS function to run when pressed on my button – user3438350 Apr 23 '15 at 12:11
0
  <div id="btn">
               <a href='dg-quiz-maker.js' class='button'>ATTEMPT A QUIZ</a>
               </div>

See the Plunker code:

http://plnkr.co/edit/YB09Puxc7qJmeW8V86nR?p=preview`

In this case the script file is in same directory with HTML

freethinker
  • 2,257
  • 4
  • 26
  • 49
-1

First of All, Opening a JS file is kind of weird thing , but still if u want to,The "href" property of <a> tag should be a proper(or relative) uri path of the file placed in your directory.

I hope it will solve your purpose.

akki_2891
  • 496
  • 1
  • 5
  • 12
  • 1
    Independently of OP getting the expected result or not, "dg-quiz-maker.js" is a valid relative path for a `href`. It's a link to the file "dg-quiz-maker.js" in the same directory as the file containing the link – Alvaro Montoro Apr 23 '15 at 11:37