0

Possible Duplicate:
jquery select iframe children

I have many frames in my asp.net website. Since I don't know its layout and I need to update a <span> value in a diferent diferent frame where my jquery code is, I do the following:

$('#myspan').text('sometext');

but anything changes. So I don't know if it's because jquery didn't manage to catch the element with this selector because is not in its scope or why.

However this:

alert($('#myspan')); it shows an alert with [object][object] as result.

Community
  • 1
  • 1
anmarti
  • 5,045
  • 10
  • 55
  • 96
  • 3
    `$(#'myspan')` looks wrong, you probably mean `$('#myspan')` – Halcyon Jan 04 '13 at 17:00
  • 2
    `alert($())` will also show `[object Object]` because `$()` always returns an object. – Kevin B Jan 04 '13 at 17:01
  • 1
    `alert($('#myspan'));` showing `[object Object]` does not mean it's matched an element. jQuery always returns an object whether it's matched something or not. If you do `alert($('#myspan').length);`, you'll probably see it's matched no elements. – Matt Jan 04 '13 at 17:01
  • I misspelled wrong. `$('#myspan')` updated!! – anmarti Jan 04 '13 at 17:02
  • 2
    See http://stackoverflow.com/questions/2893280/jquery-select-iframe-children for how to get elements within an ` – Matt Jan 04 '13 at 17:03
  • 1
    Use a browser that has a good console (webkit) and use `console.log($('#myspan'));`. This is much better than alerting objects as strings. – iambriansreed Jan 04 '13 at 17:06
  • Iambrinsreed, like this what is expected to be in the console if it works? – anmarti Jan 04 '13 at 17:09
  • @a_maar you will actually see: `['...']` in the console. saying it found 1 item with that id. – iambriansreed Jan 04 '13 at 17:11
  • Matt I don't need to get element within `iframes` but within `frames` in the site. – anmarti Jan 04 '13 at 17:32

2 Answers2

3

The jQuery function ($('someSelector')) will always return an object, even if no elements match the selector.

If you really are using multiple frames, you have a problem: jQuery can't find elements across frames, only in the current document/frame – unless you pass a proper context object for the specific document you want to target.

For example, if you have an <iframe id="myframe">, you can look for #myspan inside it with this:

var frameDoc = $('#myframe')[0].contentWindow.document;
var mySpan = $('#myspan', frameDoc);

For that to work, the iframe's source must be hosted in the same domain as the main page. There's also the following cleaner options, suggested in the comments below:

var mySpan = $("#myframe").contents().find('#myspan')

or

var mySpan = $('#myspan', $("#myframe").contents());
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
1

A good way to debug jQuery problems is this pattern:

var e = $(...);
console.log(['Explanation', e.get()]);

get() without arguments will convert the strange jQuery selector result to a normal JavaScript array which you can expand in your browser's console. That should help to see what the selector returned.

If it doesn't match anything, then your selector is wrong somehow. Check the current DOM in your browser's development tools if the element is really there.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820