Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is my mistake using JavaScript and jQuery

I have a question actually I need one if/else for hide or show one div, I had wrote the following function but it didn’t work:

jQuery(document).ready(function(){   /*show div OtraUniversidad when option:selected = 165*/
  var optionValue = $("#Universidad option:selected").val();
  $("#OtraUniversidad").hide();  
  if(optionValue == 165){
    $("#OtraUniversidad").show();
  }
});

Actually works: $("#OtraUniversidad").hide();

I don't know what's wrong; I'm new in JavaScript and jQuery

Some help is always welcome.


1 Answers

I think this should work:

jQuery(document).ready(function() {
    $('#Universidad').change(function() {
        var optionValue = $("#Universidad").val();

        if(optionValue == 165) {
            $("#OtraUniversidad").show();
        } else {
            $("#OtraUniversidad").hide();
        }
    }).change();
});

Demo: http://jsfiddle.net/gGX2E/1/

like image 67
r0skar Avatar answered Mar 31 '26 06:03

r0skar