7

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>');
});

4 Answers 4

12

Use not and has

$('#listItem:not(:has(.greenCheckMark))')
    .append('<span class="greenCheckMark"></span>');
Sign up to request clarification or add additional context in comments.

1 Comment

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 -- code.jquery.com/jquery-1.4.2.js)
1

You could just look for it?

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

EDIT: Fixed 'un-relatedness' to question.

4 Comments

Eh, why use a conditional operator when an if statement would be better suited?
@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.
I don't understand what you mean by "map"...? You're not assigning the return value of the expression to anything...
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.
0

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>');
}

2 Comments

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.
J-P +1 I like your update. Requires fewer characters than length.
0
$('.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).

1 Comment

@Eli, why this? - shouldn't it be $('#listItem').find(...?

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.