J S
J S

Reputation: 3496

JQuery .prependTo('body') not working?

It's midnight and I should just go to bed but I am utterly bamboozled. I know I'm going to feel like a total egg here, but...why isn't this working??

var galleryPanelText = '';
galleryPanelText += '<div class="galleryPanel">';
galleryPanelText += '</div>';
alert(galleryPanelText);
galleryPanelText.prependTo('body');
alert($('.galleryPanel').length);

http://jsfiddle.net/6kjKE/

As you can see, the first alert fires and the second doesn't, so the prepend line is breaking it. Thanks in advance.

Upvotes: 1

Views: 701

Answers (2)

Abhitalks
Abhitalks

Reputation: 28387

(1) In your fiddle you forgot to include jQuery!

(2) It should be $(galleryPanelText).prependTo('body');

See update fiddle: http://jsfiddle.net/6kjKE/1/

If you notice the console of your fiddle, it would show you: Uncaught TypeError: Object has no method 'prependTo'. This would give you a hint.

If you include jQuery and run, the console wil log: Uncaught TypeError: Object <div class="galleryPanel"></div> has no method 'prependTo' which means it is expecting a jQuery object, not text.

Upvotes: 1

Rohan Kumar
Rohan Kumar

Reputation: 40639

You are prepending string but prependTo() needs an jQuery object to prepend like,

$(galleryPanelText).prependTo('body');

Also add a latest version of jquery see Working Fiddle

Upvotes: 2

Related Questions