DelphiFAQ Home Search:

Programmatically call the javascript onchange event handler

 

comments10 comments. Current rating: 5 stars (7 votes). Leave comments and/ or rate it.

Question:

My javascript function updates an array of controls (listboxes) and makes selections. I would like to have the onchange event handler fire after each change but it does not get executed. How can I programatically call the handler?

Answer:

There is no special trick to this other than checking first if the event handler for the given control is actually defined - to help avoid a runtime error.

if (document.form1.listbox1.onchange)
  document.form1.listbox1.onchange();

Another observation I made is that some HTML editors will generate HTML like this:

<select .. onChange=myFunction()>

Note that the event name is in mixed case here (capitalized C) but the name of the property has to be written all lower case as shown at the top.


Comments:

2007-01-23, 13:59:32
anonymous from United States  
So,, Is there a way to fire this event programatically???
2007-08-27, 13:24:04
anonymous from Westhoughton, United Kingdom  
Did you get an answer to this problem? If so can you tell me how to solve it, I'm struggling with the same issue.
2007-09-17, 16:47:52   (updated: 2007-09-17, 16:49:24)
anonymous from New Zealand  
rating
hahahaha, ah, that cracks me up. But yeah, the answer is right. Just get hold of whatever DOM element it is you need, check that onchange is actually there (saving yourself from a possible exception) and then call onchange() on it.

E.g. you have a listbox with various regions in it and you want it to be set to 'Europe' (one of the values you loaded it with):

var regionDropdown = document.getElementById('region');
regionDropdown.value = 'Europe';
if (regionDropdown.onchange) regionDropdown.onchange();
2007-09-27, 03:32:02
[hidden] from India  
rating
It seems the question was, if the onchange event is not specified in the HTML Tag and we only have javascript to tell that HTML Tag to fire onchange event, without mentioning it in the Tag itself ??
Can we later access that Tag and attach the onchange event to it ? If yes, Can you show us an example ?
2007-10-17, 06:37:28
from Brazil  
rating
To attach a event into a control use this code

if (document.all) //IE6
{
field.detachEvent('onfocus', function);
}
else //Firefox
{
field.removeEventListener('onfocus', function, false);
}
2007-11-01, 12:09:20
anonymous  
rating
Thanks for the example on showing how to call an event in your code. Just what I was looking for...... too easy.
2008-04-17, 10:30:08
anonymous  
rating
Yep works all righty.
$('select_name').value = 'x';
$('select_name').onclick(); /* could if it exists first, too */
2008-05-22, 10:53:41
anonymous from United Kingdom  
rating
the best way is to do the whole thing programmatically and get it to execute some code is to assign the code to the event then fire it as said above - much more elegant and all the code is in the same place.
No need for any onChange attribute in the html then.

to assign :

document.element.onchange = function(){
// do code
};

or:

function DoCode(param){
//do code
}
document.element.onchange = DoCode('param');
2008-07-23, 23:45:13
anonymous from Vikhroli, India  
rating
good one...
2008-09-24, 05:51:00
anonymous from New Delhi, India  
function check()
{
var dlist=document.getElementById('DropDownName');
if(dlist.options[2].selected==true)
{

document.getElementById('PanelName').style.visibility=true;
}
else
{
document.getElementById('PanelName').style.visibility=false;
}
}

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.