Javascript dropdown needs variable name, multiple dropdowns

Posted on 2010-01-07
Last Modified: 2012-06-27
Hi Experts,

I have copied 2 snippets of code in separated by a big line.

Basically we have a javascript drop down box ($("#genre")) and where it says genre here, that has to match the id of the html of the drop down box, otherwise it doesnt work.

We have a foreach rooms as room, i.e. with have x rooms, x drop down boxes.
How can I make this code match the javascript to work x times.

Can I use a variable, perhaps the name of the room instead of genre?

If you need to see more code, please ask

make sense?  



<script type="text/javascript">
        $(document).ready(function() {
	    $("#genre").dropdownchecklist({ maxDropHeight: 120 });


<tr align="right">
                        <td colspan="2" align="left" valign="top"><table width="528" border="0" cellpadding="3" cellspacing="0">
                            <tr><th width="14%" class="tx_esq_11_gray"><strong>Artist</strong></th><th width="14%" class="tx_esq_11_gray"><strong>Time</strong></th></tr>
                            <?php foreach ($rooms as $room): ?>
			    <tr valign="top">
				<td width="100"><span class="tx_esq_14_red_c"><?php htmlout($room['roomname']); ?><br /></span> </td>
                            <td width="300" valign="middle">      
			    <select name="genres[]" multiple="multiple" id="genre">
			    <?php foreach ($genres as $genre): ?>
					<option value= "<?php htmlout($genre['id']); ?>"<?php
							if ($genre['selected'])
								echo ' selected="selected"';
							?>><?php htmlout($genre['genrename']); ?></option>
                            <?php endforeach; ?>
		  <?php endforeach; ?>

Open in new window

Question by:MWatkins84
    LVL 22

    Accepted Solution

    <select name="genres[]" multiple="multiple" id="genre">
    instead of id make it
    <select name="genres[]" multiple="multiple" class="genre">

    and then the script will be
    $(".genre").dropdownchecklist({ maxDropHeight: 120 });


    Author Closing Comment



    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now