MeelStorm
MeelStorm

Reputation: 634

jQuery can't see context

I have HTML page with several frames on it. One of frames(called Main) contains several INPUT's.

When I use this:

Main.document.getElementsByTagName('input')

It works fine. But then I try do the same in jQuery:

$("input",Main)
$("input",Main.document)
$("input",window.Main)
$("input",window.Main.document)

All of them returns null. What am I doing wrong?

UPDATE $(Main) returns null too.

UPDATE jQuery is included inside of Main frame.

at base page I have that row:

<script>$=Main.$;</script>

Upvotes: 2

Views: 663

Answers (2)

Rob W
Rob W

Reputation: 349082

jQuery cannot return null for selectors. It's very likely that you have included the wrong library, such as Mootools. You have to include jQuery at the main window. If $ is overwritten, you can still use jQuery instead of $ to use jQuery methods.

When you've made sure that you've included jQuery, use frames.Main or frames["main"], to have a readable code. Main will not point to the frame when you define a variable called Main.

It's recommended to not use the jQuery framework of the main when the frame also included jQuery, because some settings are different per window. Use:

frames["main"].$("input"); //Use the jQuery method of the frame.

Upvotes: 3

Alex Pliutau
Alex Pliutau

Reputation: 21947

$(document).ready(function() {
    $('#frame-id').contents().find('input');
});

Be sure that your iframe is currently ready.

Upvotes: 0

Related Questions