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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:08:30+00:00 2026-05-31T09:08:30+00:00

My html form has optional items to display as follows: <span class=Optional Command1 Command2>

  • 0

My html form has optional items to display as follows:

<span class="Optional Command1 Command2">
   <label for="elem1">Elem 1:</label><input type="text" id="elem1" /><br />
</span>
<span class="Optional Command1 Command3">
    <label for="elem2">Elem 2:</label><input type="text" id="elem2" /><br />
</span>

An html select element is used to select Command1, Command2 or Command3 which corresponds to the class for the above html elements.

When the select change event happens, it calls the following function:

function showOptional(commandName) {
   $('.Optional').hide();  // clear out optional inputs
   $('.Optional').filter('.' + commandName).show();  // show relvant inputs
}

However for Command1, which is included in the class for both elem1 and elem2 above, only the first input element will be displayed, but I thought the jQuery selector would apply both.

In other words, for the above html the showOptional(‘Command1’) Javascript function only displays the first span with Command1, but not the second span with Command1. Why not both?

  • 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-31T09:08:31+00:00Added an answer on May 31, 2026 at 9:08 am

    This works for me:

    function showOptional(commandName) {
       $('.Optional').hide();  // clear out optional inputsinputs
       $('.Optional.' + commandName).show();  // show relvant 
    }
    
    showOptional('Command1');​
    

    Fiddle: http://jsfiddle.net/sYXuM/1/


    However, your original approach works for me as well: http://jsfiddle.net/sYXuM/2/


    I just ran a performance test and the following code is faster than your original approach and my suggestion above:

    function showOptional(commandName) {
       $('.Optional').hide().filter('.' + commandName).show();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

HTML has an input button type to reset all fields in a form to
I have an html form that has the following structure: <input type=text name=title />
My html form has a few input text fields which can potentially get characters
This is my html form I am using. <label>Your Username:</label><input id=username name=username type=text onchange=return
I have this html form which has options: <tr> <td width=30 height=35><font size=3>*List:</td> <td
I have a html page with a form. The form has Div which gets
I have a fairly complex html form enhanced via jquery. It has multiple tabs,
This piece of valid json (it has been generated using php's json_encode): {html:form is
I have a webrequest that returns a html response which has form inside with
A form on my website's contact -us HTML page has two fields 1) subject

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.