4

I have a form with several dropdown boxes.

If user choose "Other" from the dropdown menu, another div with an input field will slide down.

The problem is that I have 20+ dropdown boxes like that. Can I create only 1 function to trigger the related one?

Here is my html :

<div class="container">
    <div>
        <select id="option1" name="city">
            <option value="">Please Choose</option>
            <option value="Other">Other</option>
            <option value="London">London</option>
        </select>
    </div>

    <div id="box1">
        <input type="text" name="city-other">
    </div>
</div>

<div class="container">
    <div>
        <select id="option2" name="color">
            <option value="">Please Choose</option>
            <option value="Other">Other</option>
            <option value="Red">Red</option>
        </select>
    </div>

    <div id="box2">
        <input type="text" name="color-other">
    </div>
</div>

And my jQuery code :

$("#box1").hide();
$('#option1').on('change', function() {
    if ( this.value == 'Other')
    {
        $("#box1").slideDown("slow");
    } else {
        $("#box1").slideUp("slow");
    }
});

$("#box2").hide();
$('#option2').on('change', function() {
    if ( this.value == 'Other')
    {
        $("#box2").slideDown("slow");
    } else {
        $("#box2").slideUp("slow");
    }
});
1
  • try my code and let me know... Commented Oct 3, 2015 at 20:51

4 Answers 4

1

Demo : http://jsfiddle.net/fn5ac1f7/

$(function () {
    $(".box").hide();
    $('.option').on('change', function() {
        var parent = $(this).parent().parent();
        var children=$(parent).children()[1];

        if ( this.value == 'Other')
        {
            $(children).slideDown("slow");
        } else {
            $(children).slideUp("slow");
        }
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use classes instead of ids and class them like:

$('.option').on('change', function() {
    if ( this.value == 'Other')
    {
        $(this).slideDown("slow");
    } else {
        $(this).slideUp("slow");
    }
});

Edit:
To hide the box, you should get the id of the select input (inside the change function) by this:

var this_id = $(this)[0].id;
this_id = this_id.split('option')[1]; //get the input id
$("#box" + this_id).hide(); //hide box with same id

5 Comments

That slides up the option, not the box.
Thank you but box is not the child of the .option.
The box doesn't have to be a child. it's enough to call the id of the box to hide it. I used the option id so I get the id number used in both the option and the box.
Could you please update your first code? I couldn't figure out how to slide down th box with your code
As I said before, your code slides/hides the option not the box.
0

just for fun demo https://jsfiddle.net/mtffje50/

$(function () {
    // hide all div with id starting with "box"
    $("div[id^='box']").hide(); 
    // observe all select with id starting with "option"
    $("select[id^='option']").on('change', function () {
        // do the trick here...
        var name = this.name.toLowerCase() + '-other';
        var $parent = $("input[name='" + name + "']").parent().parent();
        if (this.value.toLowerCase() == 'other') {
            $parent.slideDown("slow");
        } else {
            $parent.slideUp("slow");
        }
    });
});

4 Comments

Pretty complicated :)
complicated? with these few lines of code you don't have to rewrite all your html code. ( because your html code need to be rewrite you know this?)
You are right. I am trying to put the dropdown and the bos in a container to find out another way. Just updated my code.
@Dejavu: so what about my code.. it works or not? do you need more help just let me know.
0

Use classes

$(".box").slideUp();
$('.option').on('change', function() {
var parent = $($(this).parent()).parent();
var children=$(parent).children()[1];

  if ( this.value == 'Other') {
    $(children).slideDown("slow");
  } else {
    $(children).slideUp("slow");
  }
});

2 Comments

It slides the dropdown if I choose an option rather than "Other"
@Dejavu you, needed to get var parent = $($(this).parent()).parent(); for your html structure (since the container parent is a parent of parent of select), also the $(".box").slideUp(); needes to be mentioned on top. jsfiddle.net/#&togetherjs=66wOuC1roU

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.