5

I am building a simple filter feature onto my website.

Once the user selects a location from the .start-address and .end-address dropdown, and then presses #go-button, how would I only show the div's which contain a <span class="waypoint"></span> wherein the custom attribute waypoint matches either what is in .start-address and .end-address.

Please note that I want to hide the entire waypoint-detail if a match is not found in the waypoints within it, not just a single waypoint.

I have mocked up a quick jsFiddle to show this: http://jsfiddle.net/tLhdndgx/3/.

HTML

<div class="row">
    <div class="col-md-4">Start:
        <br>
        <select class="form-control start-address">
            <option value="Lab">Lab</option>
            <option value="Hall">Hall</option>
            <option value="Apartments">Apartments</option>
            <option value="Church">Church</option>
            <option value="Park">Park</option>
            <option value="College">College</option>
        </select>
    </div>
    <br>
    <div class="col-md-4">Destination:
        <br>
        <select class="form-control end-address">
            <option value="Lab">Lab</option>
            <option value="Hall">Hall</option>
            <option value="Apartments">Apartments</option>
            <option value="Church">Church</option>
            <option value="Park">Park</option>
            <option value="College">College</option>
        </select>
    </div>
    <br>
    <div class="col-md-4">
        <button type="button" class="btn btn-success" id="go-button">Go</button>
    </div>
</div>
<br>
<div class="panel panel-default waypoint-detail" style="display: block;">
    <div class="panel-body">
        <strong>Waypoint Set 1</strong>
        <br>
        <br>
        <center>
            <span style="color:#449D44">Start</span>
            <br>↓<br>
        </center>
        <center>
            <span class="waypoint" waypoint="Hall">Hall</span>
            <br>↓<br>
        </center>
        <center>
            <span class="waypoint" waypoint="Apartments">Apartments</span>
            <br>↓<br>
        </center>
        <center>
            <span class="waypoint" waypoint="Church">Church</span>
            <br>↓<br>
        </center>
        <center><span style="color:#c12e2a">Stop</span></center>
    </div>
</div>
<div class="panel panel-default rideshare-detail" style="display: block;">
    <div class="panel-body">
        <strong>Waypoint Set 2</strong>
        <br>
        <br>
        <center>
            <span style="color:#449D44">Start</span>
            <br>↓<br>
        </center>
        <center>
            <span class="waypoint" waypoint="Hall">Park</span>
                <br>↓<br>
        </center>
        <center>
            <span class="waypoint" waypoint="College">College</span>
            <br>↓<br>
        </center>
        <center><span style="color:#c12e2a">Stop</span></center>
    </div>
</div>

JavaScript

$('body').on('click', '#go-button', function (event) {
    // Collect values
    var startAddress = $('.start-address').val();
    var destinationAddress = $('.destination-address').val();
});

5 Answers 5

2

Here's a start. Simply assign the right elements to a variable and show those.

$('body').on('click', '#go-button', function (event) {
    ...
    var myDivs = $('.waypoint[waypoint=' + startAddress + ']');

    myDivs.show();
});

Demo

Notice that I've hidden the divs initially with CSS. This prevents weirdness at page load.

Here's a simplistic way to get it all. There's a way to combine those selectors, but it escapes me at the moment.

Demo 2

You'll probably want to restructure to put your arrows inside the hide/show divs.

Sign up to request clarification or add additional context in comments.

Comments

2

This should do it. http://jsfiddle.net/tLhdndgx/10/

var waypoints = $('.waypoint'),
    details = $('.waypoint-detail');

$('body').on('click', '#go-button', function (event) {
    // Collect values
    var startAddress = $('.start-address').val();
    var destinationAddress = $('.end-address').val();

    details.hide();
    waypoints.filter(function(){
        var self = $(this),
            waypoint = self.attr('waypoint');
        return (waypoint == startAddress || waypoint == destinationAddress);
    }).parent(details).show();
});

1 Comment

Sorry - I think there is some confusion. I want to hide the entire .waypoint-detail if a match is not found amongst the waypoints. Not a single waypoint.
1

You can iterate through all of the divs and if their waypoint attribute is not equal to the one set in the dropdown then hide the parent container and exit the iteration. This code checks to see if the waypoint we are at in the iteration matches the start or end destination, if it does then we exit the iteration because we have a match. If it doesn't match, we keep going until we are on the last iteration. If we are on the last iteration and still don't have a match, we simply hide the parent container.

$('#go-button').click(function() {
    var startAddress = $('.start-address').val();
    var destinationAddress = $('.end-address').val();

    function checkWaypoints(container){
        $(container).show();
        $(container+' .waypoint').each(function(a,b){
            var waypoint = $(b).attr('waypoint');
                   console.log(waypoint);
                if((waypoint == startAddress) || (waypoint == destinationAddress)){
                    return false;  
                }
                else if($((waypoint != startAddress) && (waypoint != destinationAddress)) && a == $(container+' .waypoint').length-1) {
                    $(this).closest(container).hide();
                }

         });
    }

    checkWaypoints('.waypoint-detail');
    checkWaypoints('.rideshare-detail');

});

Here is a fork of your JSFiddle with my answer: http://jsfiddle.net/w1ok0p6o/5/

8 Comments

what if user makes changes, need to unhide if they match?
Sorry - I think there is some confusion. I want to hide the entire .waypoint-detail if a match is not found amongst the waypoints. Not a single waypoint.
@charlietfl, good point. I added $('.waypoint').show() before iterating over the waypoints as a way of "resetting" the hidden ones
@methuselah, okay I have an idea, let me update my answer. You should wrap the way point set in a container that you can hide if you need to.
@methuselah i've updated my answer and added a jsfiddle
|
1

I don't really understand your UI but you can use filter()

var $waypoints = $('.waypoint').parent()

$('body').on('click', '#go-button', function (event) {
    // Collect values
    var startAddress = $('.start-address').val();
    var destinationAddress = $('.end-address').val();
    // check for at least one value
    if (startAddress || destinationAddress) {
        // hide all then filter the ones that match to show
        $waypoints.hide().filter(function () {
            var waypointVal = $(this).find('.waypoint').attr('waypoint');
            return waypointVal == startAddress || waypointVal == destinationAddress
        }).show()
    } else {
        //otherwise show all
        $waypoints.show();
    }

});

Note that <center> tag is deprecated, use css instead

DEMO

2 Comments

I second that UI comment. Wasn't sure if he wanted the arrow hidden or not.
Sorry - I think there is some confusion. I want to hide the entire .waypoint-detail if a match is not found amongst the waypoints. Not a single waypoint.
1

Here is one way to do it. You can get all of the div that don't contain a span with the class of "waypoint" and waypoint attribute equal to one of the selected options, then apply the jquery hide().

$('body').on('click', '#go-button', function (event) {
    // Collect values
    var startAddress = $('.start-address').val();
    var destinationAddress = $('.end-address').val();

    $('div').filter(function (index) {
        return $(this)
            .find("span.waypoint[waypoint=" + startAddress + "], span.waypoint[waypoint=" + destinationAddress + "]")
            .length == 0;
    }).hide();
});

jsfiddle: http://jsfiddle.net/4rvz09mu/

2 Comments

That filter is going to look at every div on the page.
@Jack The poster asked to only show the divs that contain the waypoints. If you only want to hide the waypoint-detail divs, change ($'div') in my code to $('.waypoint-detail')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.