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.

5
  • what do you mean by make appear? Commented Apr 23, 2015 at 11:30
  • There are no buttons here. That is a <div> and a nested <a> Commented Apr 23, 2015 at 11:32
  • href should do that.. Make sure the file "dg-quiz-maker.js" is in the same location/folder where you have the code file. Commented Apr 23, 2015 at 11:35
  • Is it possible that you want to run a function from the js-file when the button is pressed instead of opening the *.js as a link? And you don't actually have a real button, it's rather a link which is styled as a button. Commented Apr 23, 2015 at 11:36
  • Yes your right, sorry I couldn't find the right words for it :). I want to run a function from JS file when that button is pressed. I know its an <a> but I styled it and it looks exactly like a button. I used this tag because I thought you need href to link it to JS Commented Apr 23, 2015 at 11:44

5 Answers 5

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>

Sign up to request clarification or add additional context in comments.

Comments

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" />

1 Comment

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
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

2 Comments

I think I didn't use the right words. I mean to say I want the JS function to run not just open a JS file
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>

2 Comments

I got the idea thanks ! but how do you make a dg-quiz-makes.js function to work ?
@user3438350 you can simply include js file like this: <script src="dg-quiz-maker.js"></script> and use its functions.
-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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.