Ashish Mehta
Ashish Mehta

Reputation: 7424

Onchange Event of Select Box

I am using an onchange event of select box with jquery. On change I have given display property = none in other div. Problem is that when I got data from database and set it in select box so onchange property is not applied for given value. Why? My code

<select id="patientType" name="Patient_Type">
<option value="">--SELECT--</option>
<option value="OPD">OPD</option>
<option value="IPD">IPD</option>
</select>
<div id="roomInfo" clasas="inactive"></div>

Script

$('#patientType').change(function() {
 if ($('#patientType').val() == 'IPD') {
  if ($('#roomInfo').hasClass('inactive')) {
   $('#roomInfo').removeClass('inactive');// CSS Property Display None in this class
   $('#roomInfo').addClass('active');
  }
 } else {
  if ($('#roomInfo').hasClass('active')) {
     $('#roomInfo').removeClass('active');
     $('#roomInfo').addClass('inactive');
    }
 }
});

now i got data From database and set it this select patientType $('#patientType').val(data fetch From the database); can not apply onchange event apply and not display my roomInfo div so it is possible or not . or another method for getting that method. Please help. Thanks In Advance.

Upvotes: 1

Views: 43250

Answers (3)

Ashish Mehta
Ashish Mehta

Reputation: 7424

Get Value of Select Box and set it like this:

$("#patientType option[value='`OPD']").attr("selected","selected");

Upvotes: 1

Ashwin Preetham Lobo
Ashwin Preetham Lobo

Reputation: 1856

Since you changing the value manually. You need to trigger the .change() event of select box. Refer the following link.

http://api.jquery.com/change/

or else

if you loading the data through ajax use like following

$('#patientType').on("change",function() {
   //Your code here
});

Upvotes: 6

PanditCR
PanditCR

Reputation: 81

Set an change event in jQuery:

$("#ddl").change(function(){
    alert($(this).val());
});

Upvotes: 3

Related Questions