Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trigger/fire an attachEvent

Status
Not open for further replies.

mgh730

Programmer
Jun 12, 2002
23
US
I have an object with an event attached to it. Is there a way to trigger this event through JavaScript?

Here is an extremely simplified version of the scenario:
Code:
obj = document.createElement("select");
obj.appendChild(document.createElement("option"));
obj.appendChild(document.createElement("option"));
obj.attachEvent("onchange", function () { alert("test"); } );
document.body.appendChild(obj);

//this line should trigger the alert in the onchange event
obj.selectedIndex = 1;

When the user changes the select box above, the event fires. When JavaScript changes the selectedIndex, the event does not fire. Can I add something to the JavaScript to trigger the event? I cannot change the function inside the attachEvent line, and I cannot make the object static (like <select onchange="...code..."> )... the attachEvent code/function is part of a huge js file written by someone else.
 

Incindentally, with IE, there is no need to use a big JS library for attachEvent - it has been available to most object types since IE5.5.

You might be able to slim your code down considerably by removing this library if it is now redundant.

Dan
 
Thanks! I had looked around the msdn library, but I guess I wasn't looking in the right places.

The JS file I'm using doesn't revolve around attachEvent - it creates a series of objects that get events attached to them. I'm controlling those objects through JS - and thus I needed control over their events.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top