views:

84

answers:

4

In my code I have the following html that gets appended to a List Item when the user clicks on a specific element.

<span class="greenCheckMark"></span>

Using jQuery, how would I detect if the element existed, and if so, not add another green checkmark?

$('.clickedElement').live('click',function(){
//does the green check mark exist? if so then do nothing, else 
$('#listItem).append('<span class="greenCheckMark"></span>');
});
A: 
$('.clickedElement').live('click',function(){ 
    //does the green check mark exist? if so then do nothing, else
    if($("#listItem span.greenCheckMark").length != 0)
    {
        $('#listItem').append('<span class="greenCheckMark"></span>');
    } 
}); 

Okay, after a few edits, this should be what you're looking for.

$("#listItem span.greenCheckMark").length != 0 will return an array of JQuery objects, if that area has a length not equal to 0 then it exists. Make sure that you are checking specifically for where you want to check, ($("span.greenCheckMark").length will check the entire document for green check marks).

Alex Larzelere
@EliThompson +1 Thanks man
Alex Larzelere
@Eli, why `this`? - shouldn't it be `$('#listItem').find(...`?
J-P
Looks like I misread the question, comment removed.
EliThompson
+4  A: 

Use not and has

$('#listItem:not(:has(.greenCheckMark))')
    .append('<span class="greenCheckMark"></span>');
Adam
+1 - I like this one. No tests needed.
patrick dw
Agreed. I like this as well. No **visible** tests needed. I wonder how the performance compares to a scoped selector or the find (looking at the source trying to figure it out -- http://code.jquery.com/jquery-1.4.2.js)
tjko
A: 

You could just look for it?

($('.greenCheckMark', '#listItem').length > 0) ? /* found green check mark in #listItem */ : /* not found */ ;

EDIT: Fixed 'un-relatedness' to question.

tjko
Eh, why use a conditional operator when an `if` statement would be better suited?
J-P
@J-P, that is absolutely unimportant. Still, if you want a reason, a ternary operator allows me to easily map the binary return of the condition in a clear and concise manner.
tjko
I don't understand what you mean by "map"...? You're not assigning the return value of the expression to anything...
J-P
I realize that. But I'm explicitly stating BOTH return values. Honestly, what difference does it make? Please tell me how "an `if` statement would be better suited`. This is just an explanation to a question -- it's not going to be inserted verbatim into some code base.
tjko
A: 

As per your requirements, this will only append the <span> in the case that #listItem does not already contain a <span> with a class of greenCheckMark.

var listItem = $('#listItem');
if ( !listItem.find('span.greenCheckMark')[0] ) {
    listItem.append('<span class="greenCheckMark"></span>');
}
J-P
J-P - jQuery's `.has()` will always return an object. I get confused on this one because the method name sounds like it should return `boolean`.
patrick dw
@Patrick, thanks for reminding me :-) .. Just changed it.
J-P
J-P +1 I like your update. Requires fewer characters than `length`.
patrick dw