I am trying to select a div node based on its CSS ID, and change the div's text, all with jQuery (2.2.0). My problem is that jQuery selection never seems to happen normally?
I'll explain from this code how I have confirmed the JSNI native method renderTree() is called, the 'document ready' event fires, and the div with CSS ID is in the DOM...
public native void renderTree()/*-{
$wnd.alert("renderTree");
$wnd.$($doc).ready(function() {
$wnd.alert("DOM ready!");
$wnd.$('#gramTree').text("text changed from JSNI jQuery");
});
}-*/;
Up until now, 1.-3. all happen as expected... But the jQuery $ selection of '#gramTree' never seems to happen, as the div with that ID never has its text change to "text changed from JSNI jQuery" (its text loads from UiBinder initially as "text initialized from UiBinder", and stays that way).
I can manually get the text to change at this point by manually entering on the browser console the jQuery code line that does not seem to work from the callback ($('#gramTree').text("text changed from JSNI jQuery");
). Similarly, I can also get it to manually work by coding renderTree() to call from the click event of a button also on the page, and manually clicking that button at this point.
Any ideas what is going wrong here?
It seems by educated guess, given 4.-5., that something isn't working right with the 'document ready' event, or how how I am handling it?
Any ideas of things to further check on?
Thanks!
You should rely on View Lifecycle in order to be sure that it is attached to DOM. from GWTP ViewImpl#onAttach
doc
Method called after the view is attached to the DOM. You should override this method to perform any ui related initialization that needs to be done after that the view is attached and that the presenter doesn't have to be aware of (attach event handlers for instance)
in your case, when $wnd.$('#gramTree').text("text changed from JSNI jQuery");
is called #gramTree element is not attached to DOM and not visible to jQuery.