0

I need to add a hidden class in my JS if the span is empty, but if it isnt show the content.

HTML

<div id="uploadControls">              
  <br><span id="uploadsError" class="validErrors smarterr"></span>
</div>

JavaScript

$(document).ready(function () {
    $('#uploadControls').find('span').each(function () {
    if ($(this).is(':empty'))
        $(this).addClass('.hidden');      
});

1 Answer 1

3

A simple typo!

$(this).addClass('.hidden'); 
                  ^

The class name string has a .. addClass is not a selector, just the name[s] to be added.

It should be

$(this).addClass('hidden'); 

and you can just do it with the selector, no each/find is needed.

$("#uploadControls span:empty").addClass("hidden");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.