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

I have a select box with the onChange attribute. Now, after loading a shared page based on some conditions, I have to add 'onChange' for the javascript selection window. I am trying to use the following code. But that does not work.

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

The onChange event is not added.

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

need to change how

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

Please suggest how to add the onChange attribute.

+4
source share
4 answers

Have you tried ...

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

jQuery.on ()

+15
source
 $("#depositForm").change(function(e){ selectedMainDepositType($(this)); }); 
+6
source
  document.getElementById('depositForm').onchange = undefined; $("#depositForm").change(..); 

Take a picture .. Hope this works for you. Original post How to edit the attribute 'onchange' in the tag 'select'? (using jquery)

0
source

For those who are looking for a different answer. I used this method because I had to first search for a dynamically created selection list.

 var dropdown = document.getElementsByTagName("select"); for (i=0; i<dropdown.length; i++) { if (dropdown[i].title == "Component") { dropdown[i].onchange = changeApproverCC; } } 
0
source

Source: https://habr.com/ru/post/1435077/


All Articles