How to remove class from a dynamically created field?

Asked

Viewed 29 times

0

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

But I can’t find the label with his id;

On the console if you 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');
                }
            }
        });

1 answer

3


find works similarly to the jQuery selector. You need to define it as an id, or it will search for tags with that name. I mean, you should start with sharp (#):

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

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

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

Or, if you don’t give up being more specific:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.