ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Help with preselecting item from category dropdown menu based on URL visitor is on

January 20, 2011 3:49pm

Subscribe [4]
  • #1 / Jan 20, 2011 3:49pm

    porpoise

    30 posts

    I am using the Channel Categories Tag Category Dropdown code:

    http://ellislab.com/expressionengine/user-guide/modules/channel/categories.html#dropdown

    Once a user selects a category from the dropdown and is taken to that category page, the dropdown resets.  I would like the dropdown to preselect the category the user is on.

    This is my code:

    <form name="catmenu" action="">
                          <select name="selcat"
                       >
                            <option value="">Location</option>
                            {exp:channel:categories channel="event" style="linear" category_group="1"}
                            <option value="{path='events'}">{category_name}</option>
                            {/exp:channel:categories}
                          </select>
                        </form>
  • #2 / Jan 21, 2011 3:06am

    John Henry Donovan

    12339 posts

    porpoise,

    you could probably do something like this

    <option value="{path='events'}" {if segment_3 == {category_url_title}}selected{/if}>{category_name}</option>

    Does that help?

  • #3 / Jan 21, 2011 3:43pm

    porpoise

    30 posts

    No, that’s not working.  When I go to my base events page - index.php/events - and use that code it applies “selected” to every option even before I select anything from the dropdown.  Then when I select Chicago from the dropdown and go to /index.php/events/category/chicago, none of the dropdown options are selected!

    Do you think the Javascript in the form is screwing it up?

    (note I’ve put a space between “on” and “change” to prevent the forum from stripping out this code.

    <form name="catmenu" action="">
    
                          <select name="selcat"
                        on change ="location=document.catmenu.selcat.options[document.catmenu.selcat.selectedIndex].value;">
                            <option value="">Location</option>
                            
                            <option value="http://ice:8888/index.php/events/category/chicago" >Chicago</option>
                            
                            <option value="http://ice:8888/index.php/events/category/new-york" >New York</option>
                            
                          </select>
                        </form>
  • #4 / Jan 22, 2011 4:34pm

    Greg Salt

    3988 posts

    Hi porpoise,

    That’s because on your base events page there is no segment_3. You would need to use the code John Henry suggested and then extend it to account for that. Maybe wrapping that in a primary conditional to check for the existence of segment_3 would work for you:

    <form name="catmenu" action="">
    <select name="selcat">
    <option value="">Location</option>
    {exp:channel:categories channel="event" style="linear" category_group="1"}
    {if segment_3 != ''}
    <option value="{path='events'}" {if segment_3 == category_url_title}selected="selected"{/if}>{category_name}</option> 
    {if:else}
    <option value="{path='events'}">{category_name}</option>
    {/if}
    {/exp:channel:categories}
    </select>
    </form>

    Cheers

    Greg

  • #5 / Jan 24, 2011 3:48pm

    porpoise

    30 posts

    Ok I will try this

  • #6 / Jan 24, 2011 4:06pm

    Ingmar

    29245 posts

    Very good, let us know how it goes, please, we’ll be here.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases