10

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.

2
  • There's almost never a good reason to use any of the [on*] attributes, so don't bother. Commented Sep 19, 2012 at 14:30
  • don't think about event as you do with attributes ... in-line events are parsed at document loading and it is not meant to be edited like a classic attribute afterward Commented Sep 19, 2012 at 14:33

5 Answers 5

23

Did you try...

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

jQuery .on()

Sign up to request clarification or add additional context in comments.

1 Comment

If you need to remove the handler, use $("#depositForm").off("change");. Is the attr onChange attached when the page is generated?
6
$("#depositForm").change(function(e){
    selectedMainDepositType($(this));
});

Comments

0
 document.getElementById('depositForm').onchange = undefined;

 $("#depositForm").change(..);

Give it a shot.. I hope it works out for you.. Original post How to edit 'onchange' attribute in a 'select' tag? (using jquery)

Comments

0

For anyone looking for another answer. I used this method because I had to first search for the dynamically created select list.

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

Comments

0

You could try this:

document.getElementById("depositForm").addEventListener('change', (event) => {
 selectedMainDepositType('this');
});

Hope can help you :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.