Reputation: 1
I'm pretty new to jquery and I encountered a problem. I use $()
to make a variable a jquery object. But somehow, it does not fully work as expected (I'd expect it can access to all jquery methods for that object). Here is what I did:
$(function() {
var form = "<div id='menu-box'></div>";
$(form).appendTo('body').css({'backgroundColor': 'blue'}); // this line works
$(form).css({'backgroundColor': 'red'}); // this line does not.
});
I'm using jquery-1.9.1, is it a bug or I did something wrong.
Upvotes: 0
Views: 24
Reputation: 437386
Each time you write $(form)
, a new jQuery object is constructed that wraps the newly-created <div>
. The second time this happens, you are operating on an element that has not been inserted into the DOM.
Consolidate the two usages instead to get the desired result (although of course this does not make sense for "real" code):
var $form = $("<div id='menu-box'></div>");
$form.appendTo('body').css({'backgroundColor': 'blue'});
$form.css({'backgroundColor': 'red'});
Upvotes: 2