views:

52

answers:

2

I need to apply/remove to an input field according to a user's selection in a separate drop down form - but I can't figure out how to target the input's class.

I need to add/remove the 'pageRequired' class from this input:

<input  type="text" title="Company Required" name="customfields-tf-2-tf" class="inputclass pageRequired textInput" id="customfields-tf-2-tf" />

When the user selects one of two options from a drop down field. For example:

<select class="dropdown" name="customfields-s-1-s" id="customfields-s-1-s" >
<option value="Owner"<?php if(in_array("Owner",$temp_values)) { ?> selected='selected'<?php } ?>> Owner</option>
<option value="Broker"<?php if(in_array("Broker",$temp_values)) { ?> selected='selected'<?php } ?>> Broker</option>
</select>

If the user selects broker than I want to add the pageRequired class to the first input field and remove it if the user selects Owner.

EDIT- Ok, so here is the code I am working with:

<script type="text/javascript">
function changeClass(myDropdown) {
   if (#customfields-s-1-s.selectedIndex == 1 ) {
       $('#customfields-tf-2-tf').addClass('pageRequired');
   }
   else {
      $('#customfields-tf-2-tf').removeClass('pageRequired');
   }
}
</script>
+1  A: 

This can be done very simply using jquery:

Just add the onchange event to your dropdown and then call a function that either removes the class or adds it depending on the dropdown selection

<select class="dropdown" name="customfields-s-1-s" id="customfields-s-1-s" onchange="javascript:changeClass(this);" >

function changeClass(myDropdown) {
   if (myDropdown.selectedIndex == 1 ) {
       $('#customfields-tf-2-tf').addClass('pageRequired');
   }
   else {
      $('#customfields-tf-2-tf').removeClass('pageRequired');
   }
}

Here are the links on use:

http://api.jquery.com/addClass/

http://api.jquery.com/removeClass/

Avitus
So with jquery I can just add classes like that? I have multiple classes applied, is that going to effect it?
Thomas
it should be fine with multiple classes. Look at the links on use there is an example with multiple class add and remove.
Avitus
There's also a `.toggleClass()` that makes this even shorter: `$('#customfields-tf-2-tf').toggleClass('pageRequired', myDropdown.selectedIndex == 1);`
Nick Craver
I can't quite get this to work, have I targeted the dropdown correctly?I added the code I am using to the OP.
Thomas
Did you add the onchange to the dropdown?
Avitus
Yeah, I added this to the dropdown: `onchange="changeClass"`
Thomas
Wait I got it - added this:`onchange="javascript:changeClass(this);"`That was dumb, sorry...
Thomas
+3  A: 

If you don't want to use JQuery you can also do this with just plain JavaScript:

<script>
function changeClass(obj) {
  var input = document.getElementById("customfields-tf-2-tf");
  if(obj.value == 'Broker') {
    input.className = input.className.replace('pageRequired','');
  }
  else if(obj.value == 'Owner') {
    input.className = input.className + ' pageRequired';
  }
}
</script>

<input title="Company Required" id="customfields-tf-2-tf" class="inputclass pageRequired textInput" type="text">
<br>
<select name="matt" onchange="changeClass(this)">
<option value="Owner">Owner</option>
<option value="Broker">Broker</option>
</select>
Matthew Manela