0

I am new to web developments and learning JavaScript. In my webpage,I want to create two different buttons. And by only clicking them ,it will lead to execute the javascript codes which are stored in different files. Two seperate buttons will execute two different javascript codes. How to do that? I have a code in mywebpage.html and it works fine - enter image description here

But when I change this to enter image description here It does not work anymore. What is the issue?

9
  • 2
    Can you please share the code you have now? Maybe we can help form there! Commented May 25, 2018 at 8:35
  • It's exactly the same as with one file, you just need to load them both with your <script> tags. Commented May 25, 2018 at 8:38
  • 1
    @K.P. yeah but in that case a really nice invention called Google will also give you an answer Commented May 25, 2018 at 8:57
  • 1
    Please post your code as text, not as an image. We can't work with images of code. See Why not upload images of code on SO when asking a question? Commented May 27, 2018 at 7:00
  • 1
    What does "not work anymore" mean? Are there any errors in your console? What exactly doesn't happen and what do you want to happen? Commented May 27, 2018 at 15:49

3 Answers 3

1

It's actually the same as with one file. You simply add another one.

Let's say you have two files, file1.js and file2.js.

In file1.js, there's the function function one() {...}.
In file2.js, there's the function, as you can imagine, function two() {...}.

In your .html file, you can include those two files like this:

<script src="file1.js"></script>
<script src="file2.js"></script>

In addition to that, you have your two buttons:

<button onclick="one()">I will execute the code from the first file!</button> <button onclick="two()">I will execute the code from the second file!</button>

Now each button will execute code from different .js files.

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

1 Comment

Explain this, I am implementing visualization using JS library sigma.js . I have code in mywebpage.html like this and it works fine -
0

If your intention is to use different JavaScript code for different button then make two such functions in the same JavaScript file. Call the required function in the click handler of the particular button.

 <button id="test">Test Button</button>

  <script>    
   document.getElementById("test").addEventListener("click", function( event ) {
       // display the current click count inside the clicked div
       event.target.textContent = "click count: " + event.detail;   

   }, false);       
 </script>

Comments

0

It doesn't matter if the functions are in one or in different files, you just need to use

<script src="1.js"></script>

<script src="2.js"></script>

etc. in your html file, depends on how many js files you have .

You can create as many functions as you want. In your html file you just need to use something like this:

<button onclick="function1()"></button> 

<button onclick="function2()"></button>

etc.

A simple example: you can put the functions in two different files, and with using the code I mentioned above, it will be still working

function function1(){
document.body.style.background="green";
}
function function2(){
document.body.style.background="black";
}
<button onclick="function1()">Green background</button><br><br> <!--This is executing the green background function-->
<button onclick="function2()">Black background</button><!--This is executing the black background function-->

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.