Stejsi
Stejsi

Reputation: 15

clicking element in parent window

i have two windows.

One parent and the second an iframe. When a link is clicked inside of the iframe, i am able to effect the element in the parent window, but only certain things.

I am able to add Class without any problems, but when i want to trigger a click then it won't work.

Here is the working code for adding class:

parent window:

<a href="#/June-2013" id="page1">Name1</a>
<a href="#/Cover" id="page2">Name2</a>
<a href="#/Editorial-Cover" id="page3">Name3</a>
<a href="#/Webcast" id="page4">Name4</a>
<a href="#/Intuit" id="page5">Name5</a>

Iframe:

<div class="page5">link</div>

<script>
  $('.page5').click(function(){
     parent.top.$('#page5').addClass('classadded');
  });
</script>

But when i try

<script>
  $('.page5').click(function(){
     parent.top.$('#page5').trigger('click');
  });
</script>

nothing happens.

Any ideas?

Thanks

Jan

PS: their both on the same domain

Upvotes: 0

Views: 1148

Answers (2)

Brett Zamir
Brett Zamir

Reputation: 14345

Not a jQuery guy, but based on my testing at http://jsfiddle.net/kB3Jz/4/ , even when within the same frame, trigger is only working with jQuery-attached events, not the default click nor directly-DOM-attached events....

HTML:

<a href="http://example.com" id="page5">Name5</a>
<div class="page5">link</div>

JS:

$('.page5').click(function(){
    $('#page5').trigger('click');
});
$('#page5')[0].addEventListener('click', function () {
    alert('hello'); // Won't be triggered
});

$('#page5').click(function () {
    alert('hello2'); // Will be triggered
});

The docs do say:

"For both plain objects and DOM objects other than window, if a triggered event name matches the name of a property on the object, jQuery will attempt to invoke the property as a method if no event handler calls event.preventDefault()"

...so it would seem trigger should work (since clicking on the DOM object via $('#page5')[0].click() works), but for some reason, it doesn't--maybe it's simply that jQuery's expressed "attempt to invoke" has failed for some reason...

I'd therefore suggest changing window.location via a jQuery-added click handler on #page5

Upvotes: 1

Andy G
Andy G

Reputation: 19367

I would try

parent.location = parent.top.$('#page5').attr('href');

or

parent.location = parent.$('#page5').attr('href');

Upvotes: 0

Related Questions