Solved

Help using jquery change event for drop down list

Posted on 2008-10-16
2
24,900 Views
Last Modified: 2011-10-03
Hello,

I am trying to use a drop down box on my page. When a user selects a category from the list, I want to perform a function (filtering a table). I know the code for my table and the function to filter it is correct - it's selecting an option and running the function that I can't get to work.

I'm currently using this exact same script on another page, only instead of using the .change event, I am using .click because the categories are displayed as links.  In this case, there are so many categories that I want them to be displayed as a drop down box instead, so I was pretty sure that I just needed to change the script to use the .change event, correct? I'm sure its something very simple, but I'm still new to javascript and jquery. Thanks for your help!


drop down box html:
 
<select id="categories">
	<option>Select a Category</option>
	<option id="showAccomodations">Accommodations/Lodging/Travel</option>
	<option id="showAccounting">Accounting/Bookkeeping/Tax Services</option>
	<option id="showAdvertising">Advertising/Promotions</option>
	<option id="showAgricultural">Agricultural Equipment/Sales/Repair</option>
	<option id="showAuto_Truck_Sales">Auto/Truck Sales</option>
</select>
 
 
script:
<script type="text/javascript" src="http://www.mercerbusinessdirectory.com/jquery.dataTables.js">
</script>
<script type="text/javascript" charset="utf-8">
var filterCategory;
$(document).ready(function() {
filterCategory = $('#listings').dataTable( {
"iDefaultSortIndex": 0,
"sDefaultSortDirection": "asc",
"bPaginate": false,
"bLengthChange": false,
"bFilter": true,
"bInfo": false,
"bAutoWidth": false,
"aoData": [{ "sType": "html" },null,null]
} );
$('#listings').hide();
$('#listings_filter').hide();
$('#showAccomodations').change(function () {
	filterCategory.fnFilter( 'Accomodations/Lodging/Travel' );
	$('#listings').show();
	} );
$('#showAccounting').change(function () {
	filterCategory.fnFilter( 'Accounting/Bookkeeping/Tax Services' );
	$('#listings').show();
	} );
$('#showAdvertising').change(function () {
	filterCategory.fnFilter( 'Advertising/Promotions' );
	$('#listings').show();
	} );
$('#showAgricultural').change(function () {
	filterCategory.fnFilter( 'Agricultural Equipment/Sales/Repair' );
	$('#listings').show();
	} );
$('#showAuto_Truck_Sales').change(function () {
	filterCategory.fnFilter( 'Auto/Truck Sales' );
	$('#listings').show();
	} );
} );
</script>

Open in new window

0
Comment
Question by:holly_mbd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 400 total points
ID: 22734611
Rather than bind a change function to each of the options in selectbox, you better bind the change event to the selectbox itself.
You can use the val() method to check what the selected value is and thus filter your dataset and display the results accordingly.

Loose the id attribute for each option.
Specify an empty string to the first option in order to determine if the value is equal to ''.
Bind the change event to the selectbox.
<select id="categories">
        <option value="">Select a Category</option>
        <option>Accommodations/Lodging/Travel</option>
        <option>Accounting/Bookkeeping/Tax Services</option>
        <option>Advertising/Promotions</option>
        <option>Agricultural Equipment/Sales/Repair</option>
        <option>Auto/Truck Sales</option>
</select>
 
<script>
$('#categories').change(function () {
        if($(this).val() != '') {
                filterCategory.fnFilter($(this).val());
                $('#listings').show();
        } else
                $('#listings').hide();
        } );
} );
</script>

Open in new window

0
 

Author Closing Comment

by:holly_mbd
ID: 31506771
Perfect! That's exactly what I needed and this is much simpler. I just wasn't quite understanding how the .change event worked...thanks for enlightening me!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need help with this homework problem. 5 62
How to escape unsafe HTML tags 4 36
Html date format 9 20
JQuery DataTable Functionality 8 20
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question