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 3223058
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:03:47+00:00 2026-05-17T16:03:47+00:00

Most examples I can find showing how to work with attributes show how to

  • 0

Most examples I can find showing how to work with attributes show how to set them and not how to retrieve them. In my case, I am trying to get the class of an and then parse it to determine its parent value.

HTML:

<select class='parent' name='parent' size='10'>
    <option value='1'>Category 1 -></option>
    <option value='2'>Category 2 -></option>
</select>

<select class='child' id='child' size='10'>
    <option class='sub_1' value='5'>Custom Subcategory 1.1</option>
    <option class='sub_1' value='3'>Subcategory 1.1</option>
    <option class='sub_2' value='4'>Subcategory 2.1</option>
</select>

For any given option from the child list, I need to look at the class attribute, parse the name looking for the number (“sub_[n]”) and then grab the value of the option in the parent list.

My code so far (childVal has a value of “5” in my test case):

var class = child.find("option[value=" + childVal + "]").attr("class");

The above class variable is coming back “undefined.” I know the .find is working because I can use it for other things. The problem is getting at the class name.

Once I get the class name, I can strip out the “sub_” to get at the id number.

Is there a better way to do this? Why is .attr(“class”) returning undefined?

Here’s a Fiddle link http://jsfiddle.net/n6dZp/8/ to a broken example.

Thank you,

Rick

This is the full function I am working on. It’s a cascading select list.

<script type="text/javascript">
    function cascadeSelect(parent, child, childVal) {
        var childOptions = child.find('option:not(.static)');
        child.data('options', childOptions);

        parent.change(function () {
            childOptions.remove();
            child
                    .append(child.data('options').filter('.sub_' + this.value))
                    .change();
        })

        childOptions.not('.static, .sub_' + parent.val()).remove();

        if (childVal != '') {
            var className = child.find("option[value=" + childVal + "]").attr("class"); 

            ** need code here to select the parent and the child options based
            on childVal **
        }
    }

    $(function () {
        $('.categoryform').find('.child').change(function () {
            if ($(this).val() != null) {
                $('.categoryform').find('#CategoryId').val($(this).val());
            }
        });
    });

    $(function () {
        cascadeForm = $('.categoryform');
        parentSelect = cascadeForm.find('.parent');
        childSelect = cascadeForm.find('.child');

        cascadeSelect(parentSelect, childSelect, "5");
    });
</script>
  • 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-05-17T16:03:47+00:00Added an answer on May 17, 2026 at 4:03 pm

    To obtain the class attribute:

    var classAttribute =  $('#child').find('option[value="' + childVal + '"]:first').attr('class');
    

    To obtain the ‘n’ value you can parse the class attribute with a RegEx:

    var nValue = /\bsub_([\S]*)\b/.exec( classAttribute )[1];
    

    Hope it helps you.

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

Sidebar

Related Questions

Trying to find some simple SQL Server PIVOT examples. Most of the examples that
I have a relatively simple (I think) use-case but I can't find any examples
I often find that attributes can be too large. Sometimes it feels like the
I'm trying to figure out why occasionally I'm getting the IllegalStateException. I can't find
It seems like most examples of JPA/Hibernate entity bean classes I've seen do no
Most of time we represent concepts which can never be less than 0. For
I'm trying to make a non-model form in ruby on rails, most of the
I'm looking on how exactly I can show a loading element while the page
I find examples and tutorials about models and about validation. And I places that
Most examples of inplace editing are one-liners that iterate through a file or files,

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.