javascript - Call an event from another element - Stack Overflow

I have button A with onclick = "alert(1);"And button B.How to do, when I click on button B ,

I have button A with

onclick = "alert(1);"

And button B.

How to do, when I click on button B , it should call event on button A ?

click B -> eventB -> eventA

I have button A with

onclick = "alert(1);"

And button B.

How to do, when I click on button B , it should call event on button A ?

click B -> eventB -> eventA

Share Improve this question edited Apr 29, 2015 at 21:13 000 27.3k10 gold badges74 silver badges103 bronze badges asked Feb 9, 2011 at 2:20 ChameronChameron 2,9848 gold badges36 silver badges46 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 5

HTML:

<button id="buttonA"> Button A </button>
<button id="buttonB"> Button B </button>

JavaScript:

var a = g('buttonA'),
    b = g('buttonB');

function g(s) { return document.getElementById(s); }

a.onclick = function() {
    alert('Button A clicked!');
};

b.onclick = function() {
    alert('Button B clicked!');
    a.click();
}

Live demo: http://jsfiddle/TaPWr/1/


Update:

If the Button B is inside an IFRAME then use the top object to reference the window object of the "parent" page. The IFRAME code would be like so:

var b = document.getElementById('buttonB');

b.onclick = function() {
    alert('Button B clicked!');
    top.document.getElementById('buttonA').click();
}
onclick="document.getElementById('buttonAId').click();"

put that on button B

update: if button A is in the form that has iframe where button A is:

onclick="parent.document.getElementById('buttonAId').click();"

assuming both frames are served from the same domain

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745213333a4616972.html

相关推荐

  • javascript - Call an event from another element - Stack Overflow

    I have button A with onclick = "alert(1);"And button B.How to do, when I click on button B ,

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信