Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8996021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:41:52+00:00 2026-06-15T23:41:52+00:00

child.attr(‘selectedIndex’, 1); Doesn’t seem to work in my case. I have two cascading dropdown

  • 0
child.attr('selectedIndex', 1);

Doesn’t seem to work in my case.

I have two cascading dropdown lists, a parent and a child. To start, they both contain the same values. When a user selects an item from the parent select box, it is removed as a choice in the child select.

Sometimes, the parent select box has only 3 items, the default and 2 values. When a user selects a parent value in this case, the only possible choice in the child select is the one remaining non-default value.

So, when the parent has only two values, I’d like to automatically select the non-default value that is left in the child box when a user selects something parent select.

The HTML I start with might be as follows:

<select name="from" class="from">
<option value="" selected="selected" class="default">Select a value</option>
<option value="na">One</option>
<option value="ja">Two</option>
</select>
<select name="to" class="to">
<option value="" selected="selected" class="default">Select a value</option>
<option value="na">One</option>
<option value="ja">Two</option>
</select>

Then if the user selects “One” in the parent, the child select should automatically have “Two” selected.

Here is my jQuery I use to remove a value from the child when a value in the parent is selected:

    (function ($) {
        function cascadeSelect(parent, child) {
            var childOptions = child.find('option:not(.default)');
            child.data('options', childOptions);
            parent.change(function () {
                childOptions.remove();
                child.append(child.data('options').filter('.' + this.value))
                    .change();
                if (child.data('options').length == 2) {
                    [select the last option here]
                }

            })

            if (parent.val()) {
                childOptions.not('.default, .' + parent.val()).remove();
            }

        }


        $(function () {
            cascadeForm = $('.askform');
            pulldown_from = cascadeForm.find('.from');
            pulldown_to = cascadeForm.find('.to');

            cascadeSelect(pulldown_from, pulldown_to);
        });

I’ve added the line that checks if there are only two options in the child select:

                if (child.data('options').length == 2) {
                    [select the non-default child option here]
                }

But I can’t figure out how to select the non-default child option. I thought I could do it by using the .selectedIndex:

child.attr('selectedIndex', 1);

But this seems to reference the original index values (the indexes as they were before I have removed the value selected in the parent).

So how can I automatically select the only non-default option in the child select when there are only two options to start with in the parent?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:41:53+00:00Added an answer on June 15, 2026 at 11:41 pm

    I think I found a few issues.

    In this line:

    child.append(child.data('options').filter('.' + this.value))
                 .change();
    

    It look like you’re trying to filter using a selector for the class (not the value). You should hopefully be able to fix this by changing your selector so it filters based on the value of the option:

    child.append(child.data('options').filter('[value="' + this.value + '"]'))
                 .change();
    

    The easiest way to select a value in a dropdown is to just use val(). In other words, the line you’re after should read:

    child.val(this.value);
    

    I created a JS Fiddle if you want to see the whole thing working: http://jsfiddle.net/KkNHr/1/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why doesn't this work when a select has optgroups in it? doesn't work $(option:first-child).attr(selected,
I am using multiple checkbox with hierarchy ..i have parent checkbox and its child
I have a set of code like this: $('.rightMainP').hover(function() { $('#rightMain img:first-child').attr('src', '../Images/whiteSidewaysNub.png') },
I use rails 3.2 and I want to prevent mass-assignment. I have parent-child relationship.
Child and descendant selectors have been leaking to every element on my html structure
//child process char buf[20]; read(fd[0][0], buf, 20); printf(%s, buf); //parent process write(fd[0][1], 12, 20);
I'm developing a child theme for the Buddypress default theme. I have customised the
I'm creating a child by my nested form and I know the routes work
I have some child processes which should write some logs into a common file.
Possible Duplicate: Open child browser window from child window I have a list of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.