admin管理员组

文章数量:1244223

I have a plicated 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

I have a plicated 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

Share Improve this question edited Oct 11, 2012 at 13:56 Kaiusee asked Oct 11, 2012 at 13:28 KaiuseeKaiusee 1,3331 gold badge14 silver badges28 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 6
<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?

<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>
function sayHiB() {
    sayHiA();
}

Did you tried this with an external js ? This is quite the most basic thing you can do in javascript.

I made you a jsfiddle : http://jsfiddle/pjDVP/4/

The html :

<input id='bta' type='button' value='button a'></input>
<input id='btb' type='button' value='button b'></input>​

The js (with jquery laoded) :

$(function(){
    $('#bta').click(function(){aORbClick();});
    $('#btb').click(function(){aORbClick();});
})

function aORbClick(){alert('I clicked a or b');}

just call function sayHiA from sayHiB or call it after.

Call from sayHiB

function sayHiB()
{
   sayHiA();
}

or after

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

or easier way is to use jQuery, so you can do this

function sayHiB(){
   if($('#id-of-a').attr('onclick'))
       $('#id-of-a').click();
   else if ($('#id-of-a').attr('onmouseup'))
       $('#id-of-a').mouseUp();

}
function sayHiB(){
    $('#buttonA').click();
}

Raw JS:

function sayHiB(){
    var buttonA = document.getElementById('buttonA');
    buttonA.onclick.apply(buttonA); // in onclick function you can get buttonA as 'this'
}

I'd probably make a generic function that switches on the button's name/id to figure out what to do - this would also make your code work independent of the event attribute used to call the function.

HTML:

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

JavaScript:

function myFunc(elem){
    switch(elem.id){
        case 'buttonA':
            sayHiA();
            break;
        case 'buttonB':
            sayHiB();
            sayHiA();
            break;
    }
}

This would also help with any DOM manipulation you might need as the button which was clicked is passed to the generic function myFunc, allowing you to quickly access other attributes or nearby elements.

本文标签: javascriptCalling a function of a button from another buttonStack Overflow