DelphiFAQ Home Search:

Programmatically call the javascript onchange event handler

 

comments12 comments. Current rating: 4 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-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);
}
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;
}
}
2009-04-16, 00:03:46
anonymous from Delhi, India  
rating
it is good
This image was also posted here:
Can I disable the Refresh button in Internet Explorer



Keywords:
2012-01-18, 08:43:03
anonymous  
I have some problems in creating web page using java script.i want to make a table box where i shown only a text box with 3 fields start field,end field and depth field.i want to allow only numeric in these fields.if any alphabetic chracter enter an alert box should appear which's allow only numbers enter. And i want also a button generate table if i click it a list of tables 2 to 10 should appear on web page.please help me in creatin this
2012-07-23, 22:42:46
anonymous from United States  
2013-07-21, 03:53:23
anonymous from India  
rating



Keywords:
2013-08-14, 01:30:02
anonymous  
rating
In fact, it greatly <a href=' http://www.cheap-ma..ml'>Mac Concealer</a> improves the bike's wholesale cosmetics braking acceleration cheap mac cosmetics an.. Read Automotive Adjusting Motorcycle Chains 02nd mac makeup online December 2005 Adjusting the chains authentic mac cosmetics is a grimy task. makeup wholesale Step 01: At the very first, use your concealer from your MAC make wholesale cosmetics up collection so the eye shadow mac makeup sale has something to stick to. You just need a bit use your finger to blend it over the whole eye lid and up to <a href=' http://www.cheap-ma..ml'>Mac makeup eyeliner</a> the brow. If you have any kind of skin problem then you should use cosmetics <a href=' http://www.cheap-ma..;wholesale mac makeup</a> that have discount mac makeup been tested for sensitive skin levels of mac makeup outlet chemicals. Packaging for Safe Cosmetics As cosmetic packaging plays an important role wholesale makeup cheap makeup in how the end-user interacts with your products, an attractive package is vital.

 

 

NEW: Optional: Register   Login
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, or post under a registered account.
 

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:

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.

photo Add a picture: