0

I have a dropdown/select menu that shows and hides various divs based on what the user has selected. Within each div there is another dropdown/select menu that I would like to also trigger an action on change.

As this div is given an ID dynamically I am struggling to figure out how to select the required dropdown/select menu.

Here is the jQuery code:

$(document).ready(function(){

    $("#category").change(function(){
        var category = $("#category").val();
        var box = $('#'+ category);
        $('.class-name').hide();
        box.show();
    }) ;

    var category = $("#category").val();
    var selector = 'selector_'+category;

    $("#"+selector).change(function(){
        alert('Do something');
    });
});

And some simplified HTML:

<select id='category'>
    <option value='0'>1</option>
    <option value='1'>2</option>
    <option value='2'>3</option>
</select>

<div class='box class-name' id='1'>
    <select id='selector_1'>
        <option value='1'>One</option>
        <option value='2'>Two</option>
    </select>
</div>
<div class='box class-name' id='2'>
    <select id='selector_2'>
        <option value='1'>One</option>
        <option value='2'>Two</option>
    </select>
</div>
<div class='box class-name' id='3'>
    <select id='selector_3'>
        <option value='1'>One</option>
        <option value='2'>Two</option>
    </select>
</div>

The divs show and hide as desired, but I can't for the life of me get the alert('Do Something'); to trigger which I assume is a problem with how I am trying to select the dropdown/select menu.

All help much appreciated!

Cheers, DB.

2
  • demo is this what you want Commented Jun 28, 2016 at 3:31
  • Have you considered $('.class-name:visible').change(function(){ alert('Do something');}); ? Tell us more about the second part that's not working... Commented Jun 28, 2016 at 3:36

6 Answers 6

1

You need chain the changes. The way you made it, the code runs all at once, meaning:

  1. When document ready, do:
  2. Set onChange in the #category element
  3. Find category current value (right now, none is selected, so value is undefined)
  4. Set selector to "selector_" + undefined
  5. Find element with id selector, since there's no element with this id, no change is defined.

Try this, instead:

$(document).ready(function() {
    var categoryField = $("#category");
    categoryField.change(function() {
        var category = categoryField.val();
        var box = $('#'+ category);
        $('.class-name').hide();
        box.show();

        var selector = 'selector_'+category;

        $("#"+selector).change(function() {
            alert('Do something');
        });
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, I've accepted this answer as it remains closest to my original code and explains why my original code was not working - I needed to "chain the changes"! This has helped me achieve what I want but also helped me learn. Thanks to all the other answers also!
I'm glad it helped you. If the inside instructions of the selector "change" are always the same, there are better ways to do it, because this way is always setting the same change on an input, even if it has previously been set.
0
var category = $("#category").val();

Category is empty on page loaded.

Simple solution:

$(".box select").change(function(){
    alert('Do something');
});

Comments

0

I checked, it's working

$("#category").change(function(){    
    var category = $("#category").val();
    var box = $('#'+ category);        
    $('.class-name').hide();
    box.show();
    var s = $(box).find('select');        
    $(s).bind("change", function() { 
       alert("Do something"); 
    })
}) 

Comments

0

Within each div there is another dropdown/select menu that I would like to also trigger an action on change.

It seems you are looking to trigger event when there is a change in the dropdowns wrapped by the div

If it is so you can use jquery wild card selector

In your case all those select elements have same prefix id.

So $("[id^=selector_]") will response to to any select box which have such string as prefix

 $("[id^=selector_").change(function(event){
      $("#demoUp").text($(this).context.value);
    });

Check this JSFIDDLE. The placeholder will be updated by the value of selectd option

Comments

0

To point you have change the id on document page ,use below

$(document).on("change",$("#"+selector),function(){
        alert('Do something');
});

Comments

0

Try this:

$(document).ready(function(){

    $("#category").change(function(){
        var category = $(this).val();
        $('.class-name').hide();
        $('#'+ category).show();

        $("#selector_"+category).change(function(){
            alert('Do something');
        });
    }) ;

});

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.