Volatil3
Volatil3

Reputation: 14978

Jquery: Is there anyway to identify uniqueness of an element?

Let me explain

I have a DOM which contains element of popup windows. HTML something looks like:

<div class='editor'></div>
<div class='editor'></div>

I am working on Chrome extension that has to find all elements by class editor, if it does not find certain text, add it.

So far it's working. The DIV contains a textarea. What actually happening that if a person removes the text, it's added again. It's happening because I am using a timer to sniff whether new windows popped up.

Is there anyway I can figure out that a certain text was already added in a DIV? Does elements have some internal ID?

PS: While I am writing this question, a thing came across in my mind that, I go thru all elements have class editor and assign unique Ids as editor1,editor2..

I wonder is there any other approach?

Thanks

Update

Chrome Extension with jQuery sniff elements with classname editor, if finds or find them visible it inserts text, sayHello World. IfHello Worldalready there, it does not end. So far it's working fine. Issue is what if user manually removed **Hello World**, the code which is running undersetinterval` block adds it again. I don't want this second addition as the code should not add Hello World in element where it was already added.

Upvotes: 2

Views: 78

Answers (2)

Joe Thomas
Joe Thomas

Reputation: 6397

It doesn't seem like any browser has that feature built in. You can obviously build it yourself using I don't want to copy any answers so I'll leave you with this link: Unique identifier for HTML elements

Upvotes: 1

Tadas Paplauskas
Tadas Paplauskas

Reputation: 1903

When a user removes text (you mean textarea?) from a div, you can mark it with a custom data attribute.

element.data('skip', true);

And implement a check in your timer to skip elements with this data attribute.

if (!element.data('skip'))
{
    // do your thing
}

More info here: https://api.jquery.com/data/

Upvotes: 1

Related Questions