Javascript execute two functions at the same time with 1 (onclick event)

Javascript execute two functions at the same time with 1 (onclick event)

how to call multiple javascript functions on single event
multiple onclick events for one button
call multiple functions onclick jquery
call two functions onclick react
javascript onclick event for multiple buttons
javascript multiple functions in one script
javascript onclick multiple elements
javascript multiple functions in one function

I need to execute two js functions at the same time with one onclick event When I click the square, the menu shows but at the same time the circle inside changes his color (in this moment I have to click twice) and when I click again the square, the menu goes away and the circle change again to his default color.

https://jsfiddle.net/7bnp14vq/31/

function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

function lightOn() {
  var x = document.getElementById('light');
  if (x.style.background === 'black') {
    x.style.background = 'gold';
  } else {
    x.style.background = 'black';
  }
}
* {
  color: #FFF;
  margin: 0;
  padding: 0;
  font-size: 16px;
}

html {
  height: 100%;
}

body {
  background: #0d0d0d;
  font-family: 'Poppins', sans-serif;
}

nav {
  /*   background:blue; */
  text-align: center;
  width: 100%;
}

.nav-list {
  display: flex;
}

.nav-items {
  flex-grow: 1;
  flex-basis: 0;
  text-decoration: none;
  font-size: 1.4em;
  margin: 0 15px;
  padding: 15px 0;
  transition: 0.3s;
}

.nav-items:hover {
  border-top: 2px solid white;
  font-size: 1.6em;
  text-shadow: 2px 2px 2px #595959;
}

.nav-items:active {
  color: red;
}

.start-button {
  background: linear-gradient(to top, #abbaab, #ffffff);
  width: 165px;
  height: 165px;
  display: flex;
  margin: 15px auto;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  cursor: pointer;
}

.start-button-light {
  width: 90px;
  height: 90px;
  /*   background:#0d0d0d;  */
  background: black;
  margin: auto;
  border-radius: 45px;
  border: inset 1px #bfbfbf;
  color: white;
}

#myDropdown {
  opacity: 0;
  visibility: hidden;
  transition: 1.5s all ease-in-out;
  color: white;
}

.show {
  display: flex;
  opacity: 1 !important;
  visibility: visible !important;
}
<nav class="dropdown">

  <div onclick="myFunction(); lightOn()" class="start-button dropbtn">
    <div class="start-button-push dropbtn">
      <div id="light" class="start-button-light dropbtn"></div>

    </div>
  </div>

  <div id="myDropdown" class="nav-list">
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
  </div>
</nav>

Just combine code of both and use one function use new lightOn() I merged code for toggle in it. All functions run sequentially so its better to run them in same block.

function lightOn() {
  var x = document.getElementById('light');
  if (x.style.background !== 'gold') {
    x.style.background = 'gold';
     document.getElementById("myDropdown").classList.toggle("show");

  } else {
    x.style.background = 'black';
   document.getElementById("myDropdown").classList.toggle("show");

  }
}
* {
  color: #FFF;
  margin: 0;
  padding: 0;
  font-size: 16px;
}

html {
  height: 100%;
}

body {
  background: #0d0d0d;
  font-family: 'Poppins', sans-serif;
}

nav {
  /*   background:blue; */
  text-align: center;
  width: 100%;
}

.nav-list {
  display: flex;
}

.nav-items {
  flex-grow: 1;
  flex-basis: 0;
  text-decoration: none;
  font-size: 1.4em;
  margin: 0 15px;
  padding: 15px 0;
  transition: 0.3s;
}

.nav-items:hover {
  border-top: 2px solid white;
  font-size: 1.6em;
  text-shadow: 2px 2px 2px #595959;
}

.nav-items:active {
  color: red;
}

.start-button {
  background: linear-gradient(to top, #abbaab, #ffffff);
  width: 165px;
  height: 165px;
  display: flex;
  margin: 15px auto;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  cursor: pointer;
}

.start-button-light {
  width: 90px;
  height: 90px;
  /*   background:#0d0d0d;  */
  background: black;
  margin: auto;
  border-radius: 45px;
  border: inset 1px #bfbfbf;
  color: white;
}

#myDropdown {
  opacity: 0;
  visibility: hidden;
  transition: 1.5s all ease-in-out;
  color: white;
}

.show {
  display: flex;
  opacity: 1 !important;
  visibility: visible !important;
}
<nav class="dropdown">

  <div onclick=" lightOn()" class="start-button dropbtn">
    <div class="start-button-push dropbtn">
      <div id="light" class="start-button-light dropbtn"></div>

    </div>
  </div>

  <div id="myDropdown" class="nav-list">
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
  </div>
</nav>

Call two functions from same onclick, Closed 5 years ago. HTML & JS. How do I call 2 functions from one onclick event​? Here's my code <  To call multiple JavaScript functions in on click event, use a semicolon as shown below:onclick=Display1();Display2()Here’s the exampleLive Demo


Define a new function to call the two functions:

function onClick() {
  myFunction();
  lightOn();
}

Then use the onClick function in your onclick attribute

<div onclick="onClick()" ...

How to Call Multiple JavaScript Functions in a Single onClick Event, If you want to call or execute multiple functions in a single click event of a button, can also call more than one JavaScript function in one onclick event, like this:  How To Change Background Color Every Seconds in JavaScript. Run multiple JavaScript functions onclick. onclick is an HTML attribute. This event is triggered when mouse is clicked on it. This is an example of using onclick event. <button onclick="any_function()">Click to fire</button> You can use either function or anything.


Why not just toggle classes?

NOTE: I removed the inline click too

function toggleBoth() {
  document.getElementById("myDropdown").classList.toggle("show");
  document.getElementById("light").classList.toggle("on");
}
document.querySelector(".start-button").addEventListener("click",toggleBoth,false)
* {
  color: #FFF;
  margin: 0;
  padding: 0;
  font-size: 16px;
}

html {
  height: 100%;
}

body {
  background: #0d0d0d;
  font-family: 'Poppins', sans-serif;
}

nav {
  /*   background:blue; */
  text-align: center;
  width: 100%;
}

.nav-list {
  display: flex;
}

.nav-items {
  flex-grow: 1;
  flex-basis: 0;
  text-decoration: none;
  font-size: 1.4em;
  margin: 0 15px;
  padding: 15px 0;
  transition: 0.3s;
}

.nav-items:hover {
  border-top: 2px solid white;
  font-size: 1.6em;
  text-shadow: 2px 2px 2px #595959;
}

.nav-items:active {
  color: red;
}

.start-button {
  background: linear-gradient(to top, #abbaab, #ffffff);
  width: 165px;
  height: 165px;
  display: flex;
  margin: 15px auto;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  cursor: pointer;
}

.start-button-light {
  width: 90px;
  height: 90px;
  /*   background:#0d0d0d;  */
  background: black;
  margin: auto;
  border-radius: 45px;
  border: inset 1px #bfbfbf;
  color: white;
}

#myDropdown {
  opacity: 0;
  visibility: hidden;
  transition: 1.5s all ease-in-out;
  color: white;
}

.show {
  display: flex;
  opacity: 1 !important;
  visibility: visible !important;
}
.on { background : gold}
<nav class="dropdown">

  <div class="start-button dropbtn">
    <div class="start-button-push dropbtn">
      <div id="light" class="start-button-light dropbtn"></div>

    </div>
  </div>

  <div id="myDropdown" class="nav-list">
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
  </div>
</nav>

Call multiple JavaScript functions in onclick event, When and why to 'return false' in JavaScript? Given multiple functions, the task is to call them by just one onclick event using JavaScript. Here are few  Multiple Function Calls with One Onclick Attribute. This question comes up from time to time. And the methods I've seen on the Internet are a bit short of complete. The question is, "How do I run more than one JavaScript function with one onclick= or onsubmit= call?" and variations thereof.


Your lightOn function is not toggling correctly. #light does not have 'black' as a background to begin with, so you're off sync.

function myFunction() {
  document.getElementById("myDropdown").classList.toggle("show");
}

function lightOn() {
  var x = document.getElementById('light');
  if (x.style.background !== 'gold') {
    x.style.background = 'gold';
  } else {
    x.style.background = 'black';
  }
}
* {
  color: #FFF;
  margin: 0;
  padding: 0;
  font-size: 16px;
}

html {
  height: 100%;
}

body {
  background: #0d0d0d;
  font-family: 'Poppins', sans-serif;
}

nav {
  /*   background:blue; */
  text-align: center;
  width: 100%;
}

.nav-list {
  display: flex;
}

.nav-items {
  flex-grow: 1;
  flex-basis: 0;
  text-decoration: none;
  font-size: 1.4em;
  margin: 0 15px;
  padding: 15px 0;
  transition: 0.3s;
}

.nav-items:hover {
  border-top: 2px solid white;
  font-size: 1.6em;
  text-shadow: 2px 2px 2px #595959;
}

.nav-items:active {
  color: red;
}

.start-button {
  background: linear-gradient(to top, #abbaab, #ffffff);
  width: 165px;
  height: 165px;
  display: flex;
  margin: 15px auto;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  cursor: pointer;
}

.start-button-light {
  width: 90px;
  height: 90px;
  /*   background:#0d0d0d;  */
  background: black;
  margin: auto;
  border-radius: 45px;
  border: inset 1px #bfbfbf;
  color: white;
}

#myDropdown {
  opacity: 0;
  visibility: hidden;
  transition: 1.5s all ease-in-out;
  color: white;
}

.show {
  display: flex;
  opacity: 1 !important;
  visibility: visible !important;
}
<nav class="dropdown">

  <div onclick="myFunction(); lightOn()" class="start-button dropbtn">
    <div class="start-button-push dropbtn">
      <div id="light" class="start-button-light dropbtn"></div>

    </div>
  </div>

  <div id="myDropdown" class="nav-list">
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
    <a href="#" class="nav-items">HOME</a>
  </div>
</nav>

Is it possible to call two functions simultaneously in Javascript , will involve calling two functions at the same time by clicking on a button. I don't know if it's possible, I've only tried calling one function at a time. addEventListener("click" funcTwo); *Let me know if i said something wrong*. JavaScript doesn’t allow you to register more than one ‘onclick’ type event handler for an event. You are left with two choices: first, lump together the two function in a single wrapper


Here's the exact answer for which you're looking:

onclick="myFunction();lightOn();"

It's worth noting that there are more elegant ways to achieve this, where I think of elegant as accommodating possibly much more than two functions without compromising readability.

One such way is defining a new function, as mentioned earlier, like this:

function doBoth() {
    myFunction();
    lightOn();
}

Another is avoiding the onclick attribute entirely and instead binding event handlers to the DOM element directly, which is the preferred solution if you're a fan of unobtrusive javascript.

If they need to be sequential, then like this:

<a id="the-button" class="btn btn-primary">Click me</a>
<script>
    document.getElementById("the-button").addEventListener("click", function(){
        myFunction();
        lightOn();
    });
</script>

Otherwise, like this:

<a id="the-button" class="btn btn-primary">Click me</a>
<script>
    document.getElementById("the-button").addEventListener("click", myFunction);
    document.getElementById("the-button").addEventListener("click", lightOn)
</script>

How to call multiple JavaScript functions in onclick event?, To call multiple JavaScript functions in on click event, use a semicolon as shown below:onclick=Display1();Display2()Here's the exampleLive  Using onclick to create a dropdown button: // Get the button, and when the user clicks on it, execute myFunction. document.getElementById("myBtn").onclick = function() {myFunction()}; /* myFunction toggles between adding and removing the show class, which is used to hide and show the dropdown content */.


Multiple Functions With One Onclick, We'll use fictitious function names One(), Two(), and Three() to show how to implement the onclick attribute for calling multiple functions. There are two ways to  The two key methods to use with JavaScript are: setTimeout ( function, milliseconds) Executes a function, after waiting a specified number of milliseconds. setInterval ( function, milliseconds) Same as setTimeout (), but repeats the execution of the function continuously. The setTimeout () and setInterval


How to call 2 functions in one onclick? - JavaScript, You are left with two choices: first, lump together the two function in a single wrapper function and register it as the onclick event handler;  I was wondering, can an OnClick event of a button call multiple functions? The reason for this is I have a page that when "ok" is clicked, goes to a preview page before submitting data to the database. If "Modify" is clicked, I want to change session variables (using VB) and then go to the previous page (the easiest way I know how to


Run Two or Multiple JavaScript Functions onclick Easily , onclick. Easy way to run two or more JavaScript functions at the same time using onclick. onclick() event is very much useful to us as we can call any function on click event. But you are here to It's very similar to the previous one. Just you  both onClientClick and On_Click at same time ="javascript"> function a new window.. and at the same time OnClick event will execute and print the Hello text