John Ohara John Ohara - 1 year ago 66
jQuery Question

How to remove elements from a selector

I'm struggling to get a piece of code to work but I'm not a jquery guy so please bear with me.

I have an outer DIV ($scope)
. It contains all kinds of inputs.

I find all the entries for each input type and filter them to get the ones with values. These are stored in $entries.

$inputs contains all the inputs regardless of type or status.

What I'm trying to do is
remove $entries from $inputs
to leave the difference.

It doesn't work, and at the moment I'm not getting any errors firing back, so nothing to go on.

My first thought is that jquery is unable to match the elements in one list with the other as it just holds an index, not the actual object. This could be totally wrong (please refer back to line 1).

Either way, I need to find a way of getting all elements and segegating them into 2 bits - those with values and those without.

All help appreciated.

function inputLoaded(isPostback) {
if (typeof Page_Validators !== "undefined") {

$scope = $(".active-step:first");
$inputs = $scope.find(inputs);
$cb = $scope.find(checkboxes).filter(":checked");
$rb = $scope.find(radios).filter(":checked");
$sb = $scope.find(selects).filter(function () { return $(this).val() !== "None"; });
$ta = $scope.find(textareas).filter(function () { return $(this).val(); });
$tb = $scope.find(textboxes).filter(function () { return $(this).val(); });
$entries = $cb.add($rb).add($sb).add($ta).add($tb);

// Do things with $entries here

// Get elements that have not got entries
$el = $inputs.remove($entries);


Answer Source

The not() method can take a jQuery object whose contents will be excluded from the jQuery object you apply it to. It looks like it is exactly what you're looking for:

// Get elements, excluding entries.
$el = $input.not($entries);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download