Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 582
Daya Avatar asked Sep 19 '12 14:09

Daya


People also ask

Can I use Onchange in select tag?

Yes. Add an onChange property to the first select, then use it to call a javascript function you have written elsewhere.

How do you write Onchange in Javascript?

The onchange event occurs when the value of an element has been changed. For radiobuttons and checkboxes, the onchange event occurs when the checked state has been changed.

How do I use Onchange attributes?

Definition and UsageThe onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus.

Can we add Onchange in Div?

No; the onchange attribute is only applicable to form field elements ( input , select , textarea , etc). Thanks for your answer.


2 Answers

Did you try...

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

jQuery .on()

like image 115
JoeFletch Avatar answered Oct 18 '22 02:10

JoeFletch


$("#depositForm").change(function(e){
    selectedMainDepositType($(this));
});
like image 43
John Conde Avatar answered Oct 18 '22 02:10

John Conde