Adding li element only if it not already there?
Posted
by Legend
on Stack Overflow
See other posts from Stack Overflow
or by Legend
Published on 2010-05-30T05:57:20Z
Indexed on
2010/05/30
8:52 UTC
Read the original article
Hit count: 225
I am constructing an <li>
element like this:
var element = $("<li></li>")
.html(mHTML)
.attr('id', "elemid");
I am trying to add this element to a <ul>
element only if it doesn't already exist. Any ideas on how to do this? Am I supposed to use contains()
to see if the ul
element contain the html and then decide? For instance,
<ul id="elemul">
<li id="elemli1">Element 1</li>
<li id="elemli2">Element 2</li>
<li id="elemli3">Element 3</li>
</ul>
If I try adding Element 1, it should not add it. What should I do if its a longer string (not really long but about 150 characters).
Note: I cannot rely on IDs to determine the uniqueness. i.e. I might end up forming something like: <li id="elemli3">Element 1</li>
Do I go about using hashmaps?
© Stack Overflow or respective owner