0

I have this code on my site. The idea is to hide a specific class when a specific select box value is selected.

This is my code

$(document).ready(function(){
var txt = 'Marketing';
$("div.ginput_container select#input_3_1 option").each(function(){
    if($(this).val()==txt){
        $('.mar').hide();    
    }

   });
});

The result I'm getting is .mar class being hidden as soon as the page is loaded. I can't see the error, I have also tryied with

var num = 1

but I have the same issue.

j08691
  • 204,283
  • 31
  • 260
  • 272
Jaypee
  • 1,272
  • 5
  • 17
  • 37
  • Why are you using such a complicated selector? Just use `$("#input_3_1 option")`. Also, why are you using `.each` on a single element? – Joseph Silber Nov 13 '12 at 17:12
  • http://stackoverflow.com/questions/8572545/jquery-selection-if-option-value-equal-something-mark-a-selected I was using this thread as guide. I will try with your suggestion. – Jaypee Nov 13 '12 at 17:13
  • Is the `$(this).val()` exactly the same? As far as case? If it's `marketing` instead of `Marketing` it will not work. – Dylan Cross Nov 13 '12 at 17:14
  • Yes, they're exactly the same Dylan. – Jaypee Nov 13 '12 at 17:15
  • You're hiding `.mar` if _any_ of the options has the value `Marketing`, you're not just checking the selected option. – Barmar Nov 13 '12 at 17:16

4 Answers4

2
$(document).ready(function() {
    var txt = 'Marketing';
    $("#input_3_1").change(function () {
        if ( this.value == txt ) $('.mar').hide();
    });
});

Here's the fiddle: http://jsfiddle.net/9Cyxh/


If you want to show $('.mar') when a different option is selected, use toggle instead:

$('.mar').toggle( this.value != txt );

Here's the fiddle: http://jsfiddle.net/9Cyxh/1/


If you want this to also run on page load (before an option is manually selected), trigger the change event:

$(document).ready(function() {
    var txt = 'Marketing';
    $("#input_3_1").change(function () {
        $('.mar').toggle( this.value != txt );
    }).change();
});​

Here's the fiddle: http://jsfiddle.net/9Cyxh/2/

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • Thank you Joseph!! I added else ($('.mar')).show(); if different is selected, is that good or I should use your .toggle() ? – Jaypee Nov 13 '12 at 17:30
  • @Jaypee - It's all the same. `toggle` is just a convenience method, to make your code more concise. – Joseph Silber Nov 13 '12 at 17:31
1

You don't need the loop in the first place

Attach your select to the change() event handler and that should be it..

$(document).ready(function(){

     $("select#input_3_1").on('change', function() {
         var txt = 'Marketing';
         if(this.value === txt){
             $('.mar').hide();    
        };

     }).change()
});
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
0

If you only want to hide ".mar" class when the value is changed and it equals "Marketing" try

$("#input_3_1").change( function() {
    if( $(this).val().toUpperCase() === "MARKETING" ) {
        $(".mar").hide();
    }
});

Demo here

Bruno
  • 5,772
  • 1
  • 26
  • 43
0
$("#input_3_1").change(function(){
  if ($("#input_3_1").val()=='Marketing'){
    $(".mar").hide();
  }
});
Chris Charles
  • 4,406
  • 17
  • 31