0

I have the following HTML:

<div class="required">
<label for="personID">People</label>
<select name="personID" id="personID"  class="select">
<option value="" selected="selected" >No One Selected</option>
<option value="groupA" >Group A</option>
<option value="groupB" >Group B</option>
...

And I have this HTML:

<div class="visControl optional" style="display: none;">
<label for="refNum">Reference Number</label>
<input type="text" value="" name="refNum" id="refNum">
</div>

And I have the following SCRIPT:

<script>
  $('#personID').change(function() {
    if ($('#personID').val() == 'groupB') $('.visControl').show();
    else $('.visControl').hide();
  }); // end .change()
</script>

My script properly controls the visibility of the Reference Number block. But what I want it to also do is change the class attribute from "optional" to "required" to match the visibility.

In other words, when I show the Reference Number block I want the div.optional to become div.required and when I hide the Reference Number block I want the div.required to become div.optional

2 Answers 2

1

Use .toggleClass():

Change:

if ($('#personID').val() == 'groupB') $('.visControl').show();
else $('.visControl').hide();

to:

if ($('#personID').val() == 'groupB') $('.visControl').show().toggleClass('required  optional');
else $('.visControl').hide().toggleClass('required  optional');

jsFiddle example

Sign up to request clarification or add additional context in comments.

5 Comments

That is not working for me. I no longer get the block to show or hide
I just added a fiddle to my answer. If you watch the DOM while changing the select, you'll see the class change.
Ok, thanks @j08691 -- I see that it should work so I need to play around with it in my script. I'll post back later once I get it figured out.
awesome @j8691 -- exactly what I needed it to do. Thanks for your help.
actually @j8691 this is pretty close to what I need. After I tested it I recognized that the toggle keeps alternating the optional and required. What I actually need is required when .show() and optional when .hide() something like what [at]CamTullos has below but maybe on one line of code versus 2 lines of code
1

Try:

$('div.optional').addClass('required'); 
$('div.optional').removeClass('optional');

2 Comments

I think part of your answer @CamTullos and part of [at]j8691is what I need combined into 1. This is working but I am wondering if it cn be simplified or combined into 1 line of code if ($('#personID').val() == 'groupB') { $('.visControl').show().addClass('required'); $('.visControl').show().removeClass('optional'); } else { $('.visControl').hide().addClass('optional'); $('.visControl').hide().removeClass('required'); }
Also, what id the div class already has optional or required. Does this keep adding more like classes or does is just add them once?

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.