Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function of a button from another button

I have a complicated case here, but below is an example just to make it simple. I have two buttons, each with their own onClick function. I want to call the onClick function of button A when I click on button B.

<input id="buttonA" type="button" value="BUTTON A" onMouseUp="sayHiA()"></input>
<input id="buttonB" type="button" value="BUTTON B" onClick="sayHiB()"></input>

Note that the event can be onClick() or onMouseUp()

p.s. I have to do it using only javascript. (NO jQuery). Thanks

like image 331
Kaiusee Avatar asked Sep 15 '25 22:09

Kaiusee


2 Answers

<input type="button" onclick="fnc()"/>
<input type="button" id="message" onclick="alert('ok')" />
<script type="text/javascript">
function fnc()
{

document.getElementById("message").click();
}
</script>

are you looking for this?

like image 199
polin Avatar answered Sep 18 '25 14:09

polin


<html>
<head>//I guess something like setTimeout(function,timeInMilliseconds)
    <script language = "javascript">
    function sayHiA(){
            var v  = document.getElementById('buttonB').getAttribute("onClick");
        setTimeout(v,0);
    }
    function sayHiB(){

        document.getElementById('para').innerHTML = 'wrote';

    }
</script>

</head>
<body>

<input id="buttonA" type="button" value="BUTTON A" onMouseUp="sayHiA()"></input>
<input id="buttonB" type="button" value="BUTTON B" onClick="sayHiB()"></input>

<p id = "para">
Write Here
</p>

</body>


</html>
like image 23
Michael Shaffer Avatar answered Sep 18 '25 13:09

Michael Shaffer