0

Using javascript and/or jQuery, how do I test/match an object AND ALL of it's siblings? I'm not a programmer, so take it easy on me. Thanks in advance. :)

    // Make an array of the checked inputs
var aInputs = $('.listings-inputs input:checked').toArray();
// Turn that array into a new array made from each items value.
var aValues = $.map(aInputs, function(i){
    return $(i).val();
});
// Create new variable, set the value to the joined array set to lower case.
// Use this variable as the string to test
var sValues = aValues.join(' ').toLowerCase();

    $.each(sSplitTags, function(i){
        var re = new RegExp(sSplitTags[i],'i');
        // this test works, but not for the siblings
        if(re.test(sValues)){
        // This line does not work, but it is the gist of the question
        // if( (re.test(sValues)) && ($(re).siblings().test(sValues))){
            alert('match');
        }
        else{
            alert('no match');
        }
    });

    /*
    // Begin: There is a more elegant way to do this...
    // Create new Regular Expressions
    var re0 = new RegExp(sSplitTags[0],'i');
    var re1 = new RegExp(sSplitTags[1],'i');
    // if(thisInput.attr('value') == $(this).text()){
    if((re0.test(sValues)) && (re1.test(sValues))){
    // End: There is a more elegant way to do this...
        $(this).parent().show();
    }
    else{
        $(this).parent().hide();
    }
    */
user1003757
  • 41
  • 2
  • 8
  • 1
    Assuming `sSplitTags` is an _array_ or _object_ of _strings_, what is `sValues`? And a RegExp object doesn't have any siblings because it isn't part of the HTML DOM. – Paul S. Oct 03 '12 at 22:51
  • Why don't you try to explain what you're trying to accomplish, since you're clearly somewhat confused when describing how you're doing it. BTW, the function passed to `$.each` gets a second argument which is the element of the array, so you can use that argument instead of `sSplitTags[i]`. – Barmar Oct 03 '12 at 23:04
  • @shhac, I've added to the code to show how sValues is derived. If I use match instead, will it remain in the DOM? Thanks for the help. – user1003757 Oct 03 '12 at 23:32
  • @Barmar, I apologize if I'm not clear. I'm trying to test each item in an array and its siblings for the same condition. – user1003757 Oct 03 '12 at 23:33
  • Strings don't have siblings, so your question doesn't make any sense; only elements in the DOM have siblings. Describe what your application is trying to do, not how you're trying to do it. – Barmar Oct 03 '12 at 23:41
  • `$(re).siblings()` doesn't make any sense still; `$(re).siblings();` throws a `TypeError` because `re` is a `RegExp` and not a `HTMLElement` in the document. – Paul S. Oct 03 '12 at 23:48
  • @Barmar, thank you for your patience. I want to test all items (not siblings) in an array for the same condition. From a user experience perspective, I want to allow users to select checkboxes to filter a list of results. The results have a list of tags. All the checkboxes matching the tags must be checked for the result to show. I've added a comment to the end of the code which works, but is not scalable for more than 2 items. – user1003757 Oct 04 '12 at 00:04

1 Answers1

0

Here's how you generalize your test at the end to all elements of sSplitTags:

var showIt = true;
$.each(sSplitTags, function(i, tag) {
  re = new RegExp(tag);
  if (!re.test(sValues)) {
    showIt = false;
    break;
  }
});
$(this).parent().toggle(showIt);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for taking the time to help me work through this. The break statement throws an error, but return false can be used instead. Here's what I ended up using... `code` $.each(sSplitTags, function(i,tag){ var re = new RegExp(tag,'i'); if(!(re.test(sValues))){ show = false; // alert('no match'); return false; } else{ show = true; // alert('match'); } }); if(show == false){ $(this).parent().hide(); } else{ $(this).parent().show(); } `code` – user1003757 Oct 04 '12 at 22:19