473,385 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Can an onchange eventhandler be re-used by invoking rather than clicking?

Hi,

I'm trying to call/ mimic an onchange function/ event after a user has
clicked a checkbox. In response to which a buncch of other ones need to be
checked as well.
The caveat is that each of those child-checkboxes need to have an onchange
action take place. All of these child-boxes *have* got a working onchange
eventListener attached (thx to an old post of Lasse Reichstein Nielsen).

Following a snip that suggests (or so the debugger suggests) that the
inputElement.onchange is void, so calling onchange becomes a bit hard.
Any suggestions?

.....
inputElement = _row2checkbox[tb.rows[i]["id"]];
inputElement["checked"] = el["checked"];
if (inputElement.onchange) {
inputElement.onchange();
}
.....

I did come up with a work-around but that ivolves some serious trickery.

TIA
Fermin DCG
Jul 20 '05 #1
2 1740
Hi,

Really the answer is very simple. You didn't post the actual event handlers
you are using so I'll try to create pseudo code that explains the solution.

Your code probably looks something like:

<input type="checkbox" onchange="handleParentChange(event)" /> <!-- parent
input -->
<input type="checkbox" onchange="handleChange(event)" />

function handleChange(event){
[ perform some action ]
}
function handleParentChange(event){
[ perform some complicated action ]
}

What you need to do is seperate the code that actually does the work from
the event handler. This way you can call the code that actually performs the
work independently from a user action. So your code would change to look
like:

function handleChange(event){
var element = //get reference to the object that fired the event
doWork(element);
}

function doWork(inputElement){
[ perform some action ]
}

With this type of set up you can now call the function that performs the
actual work as many times and in as many was as you can think of becuase it
is now independent of a specific user event.

So applying it to your case your "parent" checkbox would have it's own event
handle call it handleParentChange(event) which can then build a collection
of "child" checkboxes which you then pass individually into the
doWork(inputElement) method. So putting it all together you would have
something like:

<input type="checkbox" onchange="handleParentChange(event)" /> <!-- parent
input -->
<input type="checkbox" onchange="handleChange(event)" /> <!-- child
input -->

function handleParentChange(event){
var collection = //get all child inputs
for (var i=0;i<collection.length;i++)
doWork(collection[i]);
}

function handleChange(event){
var element = //get reference to the object that fired the event
doWork(element);
}

function doWork(inputElement){
[ perform some action ]
}

I hope this wasn't too confusing.

Regards
Mike
Jul 20 '05 #2
Mike wrote:
Hi,

Really the answer is very simple. You didn't post the actual event handlers
you are using so I'll try to create pseudo code that explains the solution.

Your code probably looks something like:

<input type="checkbox" onchange="handleParentChange(event)" /> <!-- parent
input -->
<input type="checkbox" onchange="handleChange(event)" />

function handleChange(event){
[ perform some action ]
}
function handleParentChange(event){
[ perform some complicated action ]
}
Well, it doesn't actually just look like what you defined. Instead i 'slap'
on the eventHandlers in a more dynamic fashion through a js function.

In essence it does, obviously, boil down to the same thing.
What you need to do is seperate the code that actually does the work from
the event handler. This way you can call the code that actually performs the
work independently from a user action. So your code would change to look
like:
I'm still with you there.
function handleChange(event){
var element = //get reference to the object that fired the event
doWork(element);
}

function doWork(inputElement){
[ perform some action ]
}

With this type of set up you can now call the function that performs the
actual work as many times and in as many was as you can think of becuase it
is now independent of a specific user event.

So applying it to your case your "parent" checkbox would have it's own event
handle call it handleParentChange(event) which can then build a collection
of "child" checkboxes which you then pass individually into the
doWork(inputElement) method. So putting it all together you would have
something like:

<input type="checkbox" onchange="handleParentChange(event)" /> <!-- parent
input -->
<input type="checkbox" onchange="handleChange(event)" /> <!-- child
input -->

function handleParentChange(event){
var collection = //get all child inputs
for (var i=0;i<collection.length;i++)
doWork(collection[i]);
}

function handleChange(event){
var element = //get reference to the object that fired the event
doWork(element);
}

function doWork(inputElement){
[ perform some action ]
}

I hope this wasn't too confusing.

Noop, not confusing at all. As it turned out I baasically resolved in a
comparable way, so I guess its true that there is mre than one way to skin
the infamous cat ;)

I am however still surprised about the fact that one cannot utilize the
onchange in the same fashion as onsubmit. Just calling it directly from
some function (compare it so you will to doing a submit of a form via js,
or does that call *not* use the onsumbit function attached to the form?)

Thx for your effort,
Cheers,
Fermin
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: MonkeyBoy | last post by:
I am doing some some HTML manipulation in client-side script (IE5.x and IE6.x browsers only). Something like.. var tmpHTML = oTable.outerHTML // do something to the HTML her oTable.outerHTML =...
2
by: Asit | last post by:
In JavaScripts checks for an onChange event against the value of the textbox at the time of the last onChange event. Since an onChange Event never fired after you changed the text first time ,...
4
by: Bart van Deenen | last post by:
Hi all I have a script where I dynamically create multiple inputs and selects from a script. The inputs and selects must have an associated onchange handler. I have the script working fine on...
5
by: Good Man | last post by:
Hi there I'm adding form fields on the fly with some javascript DOM programming. I basically just clone a hidden <div>, then adjust node properties to make this new <div> have unique values...
3
by: b_naick | last post by:
I realize that the onChange event for a drop down can be trapped as follows: <select name="myDropDown" onChange="somefunc"> Is it possible to trap the onChange event outside of the select...
5
by: subhodey | last post by:
Hello, I have a ColdFusion online application that has a page having 2 textboxes. Corresponding to these 2 textboxes I have a Custom tag in coldfusion where the textbox is defined by <input...
4
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, <asp:DropDownList id="DropDownList1" runat="server" onchange="Test();"> can someone please tell me why the onchange has a squiggly and if there is a better way to access the...
4
by: xoinki | last post by:
hi all, I am trying to attach an event to onChange property.. the function i am trying to attach is inside a class. for eg.. my js has a structure as follows.. // if not defined a = new...
6
by: James007 | last post by:
Hi everyone, I am developing a web page for an embedded application. I created a text box for entering a value (only 1 byte long). The onchange event triggers correctly when I enter the value...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.