How to remove class from a dynamically created field?

0

I'm trying to remove a class from a label created dynamically by handlebars,

But I can not find the label with the id of it;

In the console if I pass the idLabel it returns the right id.

"9-hb_usuario-label"

But when I try to do

$('form#hb_form').find(idLabel)

To remove the class it does not.

$('form#hb_form').find('input').each(function () {

            console.log(!$(this));

            var idLabel = $(this).attr('name') + "-label";

            if (idLabel != "undefined-label") {
                if ($(this).valid()) {
                    $('form#hb_form').find(idLabel).removeClass('hide').addClass('hide');
                }
                else {
                    $('form#hb_form').find(idLabel.text()).removeClass('hide');
                }
            }
        });
    
asked by anonymous 23.05.2018 / 16:08

1 answer

3

The find works similarly to the jQuery selector. You need to define that it is an id, otherwise, it will search for tags with that name. That is, it should start with a sharp ( # ):

$('form#hb_form').find('#' + idLabel);

However, since id's are (should be) unique, and if you want to manipulate, it becomes easier:

$('#' + idLabel).removeClass('hide').addClass('hide');

Or, if you do not let go of being more specific:

$('form#hb_form #' + idLabel).removeClass('hide').addClass('hide');
    
23.05.2018 / 16:21