2

I am writing a plugin and have a question. Take these two lines into consideration

$('target').myPlugin();

var myVar = $('target').myPlugin();

Since the second target is not yet in the DOM, there is some functions I would have to change in my plugin to achieve my goals. Yet, those changes may cause the plugin to fail on the first target.

Simply, I am looking for a way (if it is even possible) to determine in my plugin if the target element is already in the DOM or in a variable.

Any help would be appreciated and thank you in advance!

UPDATE

To expound a bit...

Take this example, why does this not work?

var wrapper = $('<div/>');
var newInput = $('<input/>').wrap(wrapper);
$('#newDiv').append(newInput);

Because the input does not actually exist yet...

VIDesignz
  • 4,703
  • 3
  • 25
  • 37
  • var myVar= typeof element == 'object' ? element : document.getElementById(element); – SSA Sep 19 '14 at 13:57
  • 1
    Both of the statements you've written will look for `target` in the DOM (unless in one of them `target` is an HTML string). Your plugin can't distinguish between the two lines you have written, because they both do the same thing (one of them just happens to assigns a variable afterwards). Could you expand? – Shai Sep 19 '14 at 13:57
  • http://stackoverflow.com/questions/3426116/how-do-i-check-if-a-variable-is-a-jquery-object-or-plain-dom-element – Girish Sakhare Sep 19 '14 at 13:57
  • @GirishSakhare That is not my question... – VIDesignz Sep 19 '14 at 13:59
  • 3
    `yourVariable.closest(document).length` will tell you if the element is in the DOM or not. – Karl-André Gagnon Sep 19 '14 at 14:00
  • @Shai I Expanded a bit in my original question to show an example of when a certain function like `.wrap()` breaks when used on an object within a variable. When I asked a question a while back about why it broke, everyone said it's because the input was not actually in the DOM yet and was stored within jQuery until it is actually appended. – VIDesignz Sep 19 '14 at 14:12

0 Answers0