-3

I need to call two functions and a html page at the same time using onclick event in javascript only This is what i tried..but its not working!!

 <button type="button">
        <a href = "example.html"
        onclick="return check();
        check2();">Login </a>
</button>
brk
  • 48,835
  • 10
  • 56
  • 78
Aishwarya
  • 509
  • 1
  • 7
  • 14
  • basiclly you just have to remove the `return` but it will go to first function and when finish go to secont and not in the same time – לבני מלכה Jan 31 '19 at 06:08
  • 3
    Possible duplicate of [How to call multiple JavaScript functions in onclick event?](https://stackoverflow.com/questions/3910736/how-to-call-multiple-javascript-functions-in-onclick-event) and [Call two functions from same onclick](https://stackoverflow.com/questions/16025138) – adiga Jan 31 '19 at 06:35
  • Have you tried googling the title of your question before posting here? There are so many duplicates. Please read: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/a/261593/3082296) – adiga Jan 31 '19 at 06:37

7 Answers7

0

HTML

<button onclick="main_check()">Login</button>

Javascript

function main_check() {
    check();
    check2();
    window.location = 'example.html';
}
Vinoth RJ
  • 16
  • 2
0

Place anchor outside button:

<a href="link.html" onclick="check(); check2();">
        <button>Login</button>
</a>
0

You create a wrapper function and call the two functions there and after that you redirect to other page

<button type="button">
        <a href="#"
        onclick="wrapperFunction()">Login </a>
</button>

function wrapperFunction() {
   check();
   check2();
   window.location.href = 'example.html';
}
Rakesh Makluri
  • 647
  • 4
  • 10
0

Create a single function and inside that function call check & check1 , SInce you want to execute href , return true from the onclick event handler

function check() {
  console.log('check')
}

function check2() {
  console.log('check2')

}

function singleFnc(e) {
  check();
  check2();
  return true;
}
<button type="button"><a href = "http://www.google.com"
            onclick="singleFnc(event)">Login </a>
    </button>
brk
  • 48,835
  • 10
  • 56
  • 78
  • This has several duplicates and you have javascript gold badge. Why would you answer this? – adiga Jan 31 '19 at 06:38
0

Why don't you use one warp function contain two function call.

Mien
  • 106
  • 3
0

You can skip the onclick attribute and go straight to addEventListener. Along with allowing as many functions as you want, this technique lets you separate the concerns of structure (in HTML) and logic (in javascript).

<button id="myButton">Click me</button>
<script>
  const myButton = document.getElementById("myButton");
  myButton.addEventListener("click", function(){ check(); });
  myButton.addEventListener("click", function(){ check2(); });
</script>
Cat
  • 4,141
  • 2
  • 10
  • 18
  • (Edited to fix a syntax error) – Cat Jan 31 '19 at 06:19
  • where do we place the webpage link then ? – Aishwarya Jan 31 '19 at 06:41
  • You can redirect the browser to the desired page inside any function. So if you define a variable called `myHref` that holds the value `example.html`, you can include it in your function expression like: `function check(){ window.location.href = myHref; }` (Note that if you wanted an absolute url, your variable's value would look more like `https://example.com`). – Cat Jan 31 '19 at 14:40
0

You can call two functions like below and return the value of the function as below:

function check() {
/* Your code */
console.log("Check");
/* return the value */
return false;
}
function check2() {
/* Your code */
console.log("Check 2");
}
 <button type="button">
        <a href="example.html"
        onclick="return (function(){var ret = check(); check2(); return ret;})();">Login </a>
</button>
Ravi Sachaniya
  • 1,641
  • 18
  • 20