jeudi 28 juillet 2016

Unable to add class value using jquery .addClass()

I am using jquery to switch between classes of 2 buttons. When I try to insert a class to button attribute. It does not add all of the values. When use addClass(), it does not specifically add deleteMultipleSearch Class. And when I try to add that class while on load, it works even when the button is disabled, which should not.

Here is my code:

HTML CODE:

<table>
    <td>
        <a class="btn red searchDelete" data-id="" data-name="">Delete</a>
    </td>
    <td>
        <div class="row">
            <div class="switch">
                <label>
                    <input type="checkbox" name="ids" class="id-search" value="">
                    <span class="lever"></span>
                </label>
            </div>
        </div>
    </td>
</table>

<a class="btn" id="deleteMultipleSearch">Delete Selected</a>

HERE IS JQUERY CODE:

$('.id-search').on('change',function () {
var ids = [];
$('input:checkbox[name=ids]:checked').each(function () {
    ids.push($(this).val());
});
var length = ids.length;
if (length > 0) {
    $('.searchDelete').addClass('disabled').removeClass('waves-effect waves-light red');
    $('#deleteMultipleSearch').addClass('waves-effect waves-light red deleteMultipleSearch').removeClass('disabled');
}
else {
    $('.searchDelete').addClass('waves-effect waves-light red').removeClass('disabled');
    $('#deleteMultipleSearch').addClass('disabled').removeClass('waves-effect waves-light red deleteMultipleSearch');
}
});

$(document).ready(function () {
    var ids = [];
    $("input:checkbox[name=ids]:checked").each(function () {
    ids.push($(this).val());
    });

    if (ids.length < 1) {
    $('#deleteMultipleSearch').addClass('disabled').removeClass('deleteMultipleSearch');
    //   delete multiple button will be disabled
    } else {
    $('#deleteMultipleSearch').addClass('waves-effect waves-light red deleteMultipleSearch').removeClass('disabled');
    // enable delete multiple
    $('.searchDelete').removeClass('waves-effect waves-light btn red');
    //    disable single delete button
    }
});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire