How to add onChange attribute for select box from Javascript / jQuery

54,399

Solution 1

Did you try...

$("#depositForm").on("change", function(event) { 
     selectedMainDepositType(this);
} );

jQuery .on()

Solution 2

$("#depositForm").change(function(e){
    selectedMainDepositType($(this));
});
Share:
54,399
Daya
Author by

Daya

Works for Infosys, Bangalor .

Updated on July 04, 2020

Comments

  • Daya
    Daya almost 4 years

    I have select box with out onChange attribute. Now after loading the total page, based on some conditions I have to add onChange for the select box from Javascript. I am trying with the following code, but its not working:

    $("#depositForm").attr(("onChange", "selectedMainDepositType('this');" ));
    

    Its is not adding the onChange event.

    <select id="depositForm_depositSubType" class="depositSubType" style="width:160px;" name="depositSubType"> 
    

    has to be changed as

    <select id="depositForm_depositSubType"  onchange="selectedMainDepositType(this);"  class="depositSubType" style="width:160px;" name="depositSubType">
    

    Please suggest how to add the onChange attribute.