0

I previously posted a script to dynamically filter my select menus (http://stackoverflow.com/questions/9516736/why-does-my-jquery-filter-only-work-once-on-this-select-element)

Now I'm having a bit of a problem.

I'm trying to allow the user to:

  1. Select a Colour
  2. Which then displays the Size options, relating to that colour in another Select Menu
  3. BUT also display the FIRST option value (from the Size Select Menu) somewhere else on the page every time you change a colour.

This is the script I have:

    var $options= $('#product-multiple-2 option');
    $("#product-multiple-1").change(function(){
        // Change Image
        var pImage3 = "";
        $("#product-multiple-1 option:selected").each(function () {
            pImage3 = $(this).attr("data-product-image"); + " ";
            $(".product-code").html('');
            $("#product-multiple-container").fadeOut();
        });
        $("#slideshow-image").attr("src", pImage3);
        // Display Options
        $("#product-multiple-container").fadeIn();
        var matches = $options.filter('.'+$(this).val()).clone();
        $("#product-multiple-2").html(matches);
    });

And this is the HTML

            <p class="product-code"></p>
            <li>
            Colour Options
            <select name="item_options[cf_product_option_colour]" id="product-multiple-1">
                <option>Choose Option</option>
                <option value="Red">Red</option>
                <option value="Green">Green</option>
                <option value="Blue">Blue</option>
            </select>
            </li>
            <li>
            Size Options
            <select name="item_options[cf_product_option_size]" id="product-multiple-2">
                <option>Choose Option</option>
                <option value="75643" class="Red">5ml - £1.00</option>
                <option value="765432" class="Red">10ml - £2.00</option>
                <option value="867564534" class="Red">15ml - £3.00</option>
                <option value="5434" class="Red">20ml - £4.00</option>
                <option value="6453234" class="Green">5ml - £1.00</option>
                <option value="45256536" class="Green">10ml - £2.00</option>
                <option value="52454" class="Green">15ml - £3.00</option>
                <option value="6543754" class="Blue">5ml - £1.00</option>
                <option value="4316243" class="Blue">10ml - £2.00</option>
            </select>
            </li>

Note from my HTML, I have a class called "product-code" this is where I want to place the Value from the Size options.

The problem im having, is if I use the :first selector. It throws me the first option from the list of options that were there previously. Not when they have been updated.

1
  • Could you please stick it in jsfiddle.net so we can have a play. Cheers. Commented Mar 21, 2012 at 11:58

2 Answers 2

1

Wouldn't putting this at the end of the change handler do the trick?

$(".product-code").html($("#product-multiple-2 option:selected").val());
Sign up to request clarification or add additional context in comments.

1 Comment

Spot on, this worked. Why did i not think of this?! So simple. I did need to make sure i placed it AFTER it had filtered the options though, like below:
0
            var $options= $('#product-multiple-2 option');
            $("#product-multiple-1").change(function(){
                // Change Image
                var pImage3 = "";
                $("#product-multiple-1 option:selected").each(function () {
                    pImage3 = $(this).attr("data-product-image"); + " ";
                    $(".product-code").html('');
                    $(".product-multiple-container").fadeOut();
                });
                $("#slideshow-image").attr("src", pImage3);
                // Display Options
                $(".product-multiple-container").fadeIn();
                var matches = $options.filter('.'+$(this).val()).clone();
                $("#product-multiple-2").html(matches);
                $(".product-code").html($("#product-multiple-2 option:selected").val());
                $(".product-price").html($("#product-multiple-2 option:selected").attr("data-product-price"));
            });

Comments

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.