views:

273

answers:

2

When developing a Windows Vista/7 Gadget, and putting out text using addTextObject(), this works:

var mytext = "Hello";
document.getElementById("background").addTextObject(mytext , "Verdana", 11, "white", 10, 10);

But I cant get this jQuery selector into a variable to work:

var mytext = $("#myid").text();
document.getElementById("background").addTextObject(mytext , "Verdana", 11, "white", 10, 10);

It doesn't work, it only writes an empty string. Why?

A: 

I found out why. The content in my div was added dynamically using jQuery.

<div id="myid"></div>

$("#myid").append("somedata"); 
var mytext = $("#myid").text();
System.Debug.outputString(mytext); // Empty

For some reason, this doesn't work in the Gadget. It does work in a plain HTML file though...

Static content do work in the Gadget:

<div id="myid">somedata</div>

var mytext = $("#myid").text();
System.Debug.outputString(mytext); // Ok

Can anyone explain how I can query dynamically added content in a Sidebar? UPDATE: This solved my problem.

Magnus Johansson
I've had cases where I had to relinquish control in order to allow the browser to do its work. To do this, I put the code into a function called by setTimeout().
Nosredna
I found a workaround, see here: http://stackoverflow.com/questions/923489/is-there-a-way-to-query-html-xml-content-from-a-variable-using-jquery
Magnus Johansson
A: 

Try $(document).ready


$(document).ready(function() {
    $("#myid").append("somedata"); 
    var mytext = $("#myid").text();
    System.Debug.outputString(mytext);
});

Maybe your code is running before the DOM completes.

Renato Aquino