Fred J. Fred J. - 6 months ago 31
jQuery Question

combining jQuery selectors

My IDE is complaining about in-efficient use of jQuery selector and I am wondering if there is a better way to do this. Thanks



$('#login2').addClass('inactive');
$('#login2>input').attr("disabled", 'true');

Answer

Assign $('#login2') to var:

var $login = $('#login2');

$login.addClass('inactive');
$login.find('input').prop('disabled', true);

Should also be able to chain, but (to me) can get messy:

$login.addClass('inactive').find('input').prop('disabled', true);