user1671731
user1671731

Reputation: 247

Get iframe by id

I'm a little confused with the following code :

HTML :

<div class="playerContainer">
    <iframe id="playerFrame" name="playerFrame" frameborder="0"></iframe>
<div class="playerContainer">

Javascript :

var playerFrame1 = $("#playerFrame");
var playerFrame2 = window.frames["playerFrame"];
alert(playerFrame1); // returns [object object]
alert(playerFrame2); // returns [object Window]

My first question is why we get 2 different outputs.

When I try to embed a webpage using playerFrame1 as ID, nothing happens. But everything work fine with playerFrame2. Anybody knows why ?

Thank you.

PS : I have also tried

$('playerFrame'), $(iframe[name="playerFrame"]), document.getElementById("playerFrame")

No change.

Upvotes: 4

Views: 2197

Answers (2)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382474

The first one isn't a DOM object but a jQuery one. It gives you access to the DOM object (or a collection of DOM objects), and methods to read or modify its state, but also doesn't offer all the methods of the DOM object(s).

This is usually called a jQuery wrapped object.

If you need to get the DOM object from a jQuery one, you may use

var obj = $obj.get(0);

Upvotes: 0

Tim Wintle
Tim Wintle

Reputation: 2433

This is really a jQuery question (I'm assuming that $ is the jQuery object).

jQuery returns an array of objects - where window.frames is a mapping to Window objects.

Upvotes: 1

Related Questions