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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:02:10+00:00 2026-05-26T09:02:10+00:00

I’ve got some code that looks like this. The values of select#member_id get added

  • 0

I’ve got some code that looks like this. The values of select#member_id get added dynamically by PHP. I’m trying to determine whether the user selects a res or com option from the list, which will determine whether I dynamically add in a second dropdown. With the code as is, I consistently get -1 alerted by the script. The console.log call prints out: [1, 3] [2].

<script>
var res_members = new Array();
var com_members = new Array();
</script>
<p><select name="member_id" id="member_id">
    <option value=""></option>
    <option value="1">Alice (res)</option><script>res_members.push(1);</script>
    <option value="2">Bob (com)</option><script>com_members.push(2);</script>
    <option value="3">Carl (res)</option><script>res_members.push(3);</script>
</select></p>
<script>
    console.log(res_members, com_members);
    $(function(){
        $('#member_id').change(function(){
            alert($.inArray($('#member_id').val(), res_members));
            alert($.inArray($('#member_id').val(), com_members));
        });
    });
</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-26T09:02:10+00:00Added an answer on May 26, 2026 at 9:02 am

    Why don’t you go with something simpler and more convenient like this:

    <p><select name="member_id" id="member_id">
        <option value=""></option>
        <option class="res" value="1">Alice (res)</option>
        <option class="com" value="2">Bob (com)</option>
        <option class="res" value="3">Carl (res)</option>
    </select></p>
    <script>
        $(function(){
            $('#member_id').change(function(){
                var isRes = $(this).find(':selected').hasClass('res');
                alert(isRes ? "res" : "com");
            });
        });
    </script>
    

    See it in action.

    Update: Why your current code doesn’t work

    It’s because you are pushing integers into the array (with push), while you are searching for strings with $.inArray. Changing either of these would make the existing code work.

    So one solution would be to push in strings:

    <option value="1">Alice (res)</option><script>res_members.push("1");</script>
    <option value="2">Bob (com)</option><script>com_members.push("2");</script>
    <option value="3">Carl (res)</option><script>res_members.push("3");</script>
    

    And another would be to look for integers:

    alert($.inArray(parseInt($(this).val()), res_members));
    alert($.inArray(parseInt($(this).val()), com_members));
    

    Evidently $.inArray compares haystack elements with the needle using operator ===.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and

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.