1

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

0

2 Answers 2

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.net/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();
}
Sign up to request clarification or add additional context in comments.

5 Comments

if Button B is in a iframe and button A is out of iframe ? how
@Chameron Do both pages comply with the Same Origin Policy? en.wikipedia.org/wiki/Same_origin_policy
@Chameron Use top to get the parent window. See my updated answer.
I have a question, what's the difference between top and parent?
@JCOC "Where the window.parent property returns the immediate parent of the current window, window.top returns the topmost window in the hierarchy of window objects." from here: developer.mozilla.org/en/DOM/window.top
1
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

2 Comments

if Button B is in a iframe and button A is out of iframe ? how ?
same way, just use the respective DOMs (document objects), however if the iframes are server from different domains you will not be able to do it in most up to date browsers

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.