5

I create an element dynamically:

var $el = $('<div class="someclass"></div>');

I want to append the element ($el) somewhere, but it shouldn't have an id.

How do I know if it has been appended before or not?

edit
I thought this should work

if($($el, "#target").length == 0)
    $("#target").append($el);

but that was wrong

2
  • You can write that like this too: var $el = $("<div>").addClass("someclass"); Commented Jan 8, 2012 at 12:54
  • 1
    @micha or as var $el = $('<div>', { 'class': 'someClass'} ); Commented Jan 8, 2012 at 13:00

4 Answers 4

6

You can simply check to see if it has a parent:

var $el = $('<div class="someclass"></div>');
//Code you wrote that may or may not attach it to the DOM

if($el.parent().length === 0) {
    //The element has not been added to the DOM because it doesn't not have a parentNode
}

However, if you have no idea what is actually being done with the element, you may have other problems with your code.

Sign up to request clarification or add additional context in comments.

Comments

4

If I understand, and you need to check if another <div class='someclass'> already exists before appending $el, you can do:

if ($("div.someclass").length === 0) {
  // it has not been created yet, create a new one
  var $el = $('<div class="someclass"></div>');
}

To check if it is already a child node of some other element, use .find()

if ($("#parentNode").find("div.someclass").length === 0 {
  // it has not been created yet, create a new one
  var $el = $('<div class="someclass"></div>');
}

4 Comments

.someclass class may be used for other element, i just want to perform a check about this element with the same class.
@OmidAmraei You can test the containing element to see if it is already there using .find("div.someclass")
there could be some another elements with someclass in container
@OmidAmraei Then you are going to need a better selector, such as an id attribute to find a more specific element.
1

If I understand you correctly, you wish to find whether this exact element is already in the DOM, rather than some other arbitrary element that happens to have the same class.

If so, I believe this will work.

First, get the originally created element as an HTMLElement object rather than a jQuery object:

var el = $el.get(0);

then try to .find() it in the doc:

var $match = $(document).find(el);
var found = ($match.length > 0);

this won't be efficient, though - restrict the selector document to a narrower part of your DOM if you can!

Your edit suggests you could use #target as that selector.

8 Comments

@OmidAmraei then please knock out a minimal test case on jsfiddle.net that shows it.
@OmidAmraei p.s .find(element) requires jQuery 1.6 or later
I user jQuery 1.7 dear Alnitak
This doesn't make any sense, he is creating the element out of thin air, therefore the exact same element cannot be in the dom.
@Esailija I was assuming that there was some (omitted) code that may have added $el and that he needs to know later whether that is indeed the case.
|
-1

If you only want one element, you should really be using an ID with the element, and then checking for it in a similar way to how Michael has above. using

var el = $('<div id="someID" class="someClass" />');

I'm also not sure why you are using $el instead of just el

1 Comment

using $el is a convention some programmers use to indicate that the variable is a jQuery object and not a native element. It reminds them that they needn't write $($el).method() and can just write $el.method() instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.