473,399 Members | 3,888 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,399 software developers and data experts.

Appending Events

Hi,

I have a situation where I have a button with an onclick event
containing a function and I need to append a function to that event.

For example, I have:

<button id="btn" onclick="func1(param);">The Button</button>

When the user clicks another button, I want to essentially have the
button above look like:

<button id="btn" onclick="func1(param);func2(param);">The Button</
button>

Is this possible? If so, can I get some guidance, please?
Thanks,

Tim

Jun 27 '08 #1
9 1056
Ugo
I have a situation where I have a button with an onclick event
containing a function and I need to append a function to that event.

For example, I have:

<button id="btn" onclick="func1(param);">The Button</button>

When the user clicks another button, I want to essentially have the
button above look like:

<button id="btn" onclick="func1(param);func2(param);">The Button</
button>

Is this possible?
Yes

e.g.

<button id="btn2" onclick="document.getElementById('btn').onclick =
function(){ func1(param);func2(param); }">change</button>
Jun 27 '08 #2

Thank you for your quick and informative answer. However, I failed to
mention a very important piece of the puzzle. The button to which I
need to add the new event is going to be created in the same onclick
event. I'm finding out in that case, javascript doesn't even
recognize the new component until the page refreshes. So, I can't do
this without changing the function that adds the button, which I'm not
allowed to do.

Oh well, I'll continue banging away at it.
On May 5, 10:24 am, Ugo <priv...@nospam.itwrote:
I have a situation where I have a button with an onclick event
containing a function and I need to append a function to that event.
For example, I have:
<button id="btn" onclick="func1(param);">The Button</button>
When the user clicks another button, I want to essentially have the
button above look like:
<button id="btn" onclick="func1(param);func2(param);">The Button</
button>
Is this possible?

Yes

e.g.

<button id="btn2" onclick="document.getElementById('btn').onclick =
function(){ func1(param);func2(param); }">change</button>

Jun 27 '08 #3
Ugo
>>I have a situation where I have a button with an onclick event
>>containing a function and I need to append a function to that event.
For example, I have:
<button id="btn" onclick="func1(param);">The Button</button>
When the user clicks another button, I want to essentially have the
button above look like:
<button id="btn" onclick="func1(param);func2(param);">The Button</
button>
Is this possible?

Yes
e.g.
<button id="btn2" onclick="document.getElementById('btn').onclick =
function(){ func1(param);func2(param); }">change</button>
Thank you for your quick and informative answer. However, I failed to
mention a very important piece of the puzzle. The button to which I
need to add the new event is going to be created in the same onclick
event. I'm finding out in that case, javascript doesn't even
recognize the new component until the page refreshes. So, I can't do
this without changing the function that adds the button, which I'm not
allowed to do.

Oh well, I'll continue banging away at it.
look if I understand...

you have a button so:
<button id="btn" onclick="func1(param);">The Button</button>
and you want it becomes so:
<button id="btn" onclick="func1(param);func2(param);">The Button</button>
after first click, is it right?

if so, and if you may append a function firstly:
<button id="btn" onclick="func1(param);changeEvent(this)">
The Button</button>

function changeEvent(elem)
{
elem.onclick = function()
{
func1(param);func2(param);
}
}
Jun 27 '08 #4
On May 5, 11:52 am, Ugo <priv...@nospam.itwrote:
>I have a situation where I have a button with an onclick event
containing a function and I need to append a function to that event.
For example, I have:
<button id="btn" onclick="func1(param);">The Button</button>
When the user clicks another button, I want to essentially have the
button above look like:
<button id="btn" onclick="func1(param);func2(param);">The Button</
button>
Is this possible?
Yes
e.g.
<button id="btn2" onclick="document.getElementById('btn').onclick =
function(){ func1(param);func2(param); }">change</button>
Thank you for your quick and informative answer. However, I failed to
mention a very important piece of the puzzle. The button to which I
need to add the new event is going to be created in the same onclick
event. I'm finding out in that case, javascript doesn't even
recognize the new component until the page refreshes. So, I can't do
this without changing the function that adds the button, which I'm not
allowed to do.
Oh well, I'll continue banging away at it.

look if I understand...

you have a button so:
<button id="btn" onclick="func1(param);">The Button</button>
and you want it becomes so:
<button id="btn" onclick="func1(param);func2(param);">The Button</button>
after first click, is it right?
Sorry, that's not right.

What I have to begin with is button A only.
User clicks button A.
In the onclick event for button A I have a function that creates
button B. The function that creates button B attaches an onclick
event to button B. After button B is created (while still in the
onclick event handler) I need to add another function to button B's
onclick event.

Sorry if this isn't clear, but it's a strange requirement.

Thanks for trying to help.

if so, and if you may append a function firstly:
<button id="btn" onclick="func1(param);changeEvent(this)">
The Button</button>

function changeEvent(elem)
{
elem.onclick = function()
{
func1(param);func2(param);
}

}
Jun 27 '08 #5
Ugo
[cut]
Sorry, that's not right.

What I have to begin with is button A only.
User clicks button A.
In the onclick event for button A I have a function that creates
button B. The function that creates button B attaches an onclick
event to button B. After button B is created (while still in the
onclick event handler) I need to add another function to button B's
onclick event.
ok, maybe I'm close to understand :)
do you know create a new button? (You can think of creating hidden and
showing on event)
make me see how you could make it...

Then, if I understood correctly, while we are creating/showing the new
button, we must to associate to own onclick 1/2 function|s, is it right?
when the second? together?
Jun 27 '08 #6
What I have to begin with is button A only.
User clicks button A.
In the onclick event for button A I have a function that creates
button B. *The function that creates button B attaches an onclick
event to button B. *After button B is created (while still in the
onclick event handler) I need to add another function to button B's
onclick event.

Sorry if this isn't clear, but it's a strange requirement.

Thanks for trying to help.
If something like

buttonb.onclick = (function(original) {
return function() {
original();
new_function();
};
})(buttonb.onclick);

doesn't work, would using the level 2 DOM handlers work for you? i.e.
buttonb.addEventListener('click', new_func);
Jun 27 '08 #7
On May 5, 7:13 pm, apatheticagnostic <apatheticagnos...@gmail.com>
wrote:
What I have to begin with is button A only.
User clicks button A.
In the onclick event for button A I have a function that creates
button B. The function that creates button B attaches an onclick
event to button B. After button B is created (while still in the
onclick event handler) I need to add another function to button B's
onclick event.
Sorry if this isn't clear, but it's a strange requirement.
Thanks for trying to help.

If something like

buttonb.onclick = (function(original) {
return function() {
original();
new_function();
};
})(buttonb.onclick);

doesn't work, would using the level 2 DOM handlers work for you? i.e.
buttonb.addEventListener('click', new_func);

That sounds like an interesting alternative. However, I'm finding
that I can't even access button B while I'm still inside button A's
onclick event handler because it was only just created by the previous
function and not yet accessible, so I think it is not possible this
way. I need to somehow refresh the page before accessing button B.

Thanks for all of your great ideas, I appreciate them.

Jun 27 '08 #8
On May 6, 7:06*am, dunerunner <tpdi...@gmail.comwrote:
On May 5, 7:13 pm, apatheticagnostic <apatheticagnos...@gmail.com>
wrote:
What I have to begin with is button A only.
User clicks button A.
In the onclick event for button A I have a function that creates
button B. *The function that creates button B attaches an onclick
event to button B. *After button B is created (while still in the
onclick event handler) I need to add another function to button B's
onclick event.
Sorry if this isn't clear, but it's a strange requirement.
Thanks for trying to help.
If something like
buttonb.onclick = (function(original) {
* * * * * * * * * * *return function() {
* * * * * * * * * * * *original();
* * * * * * * * * * * *new_function();
* * * * * * * * * * *};
* * * * * * * * * * })(buttonb.onclick);
doesn't work, would using the level 2 DOM handlers work for you? i.e.
buttonb.addEventListener('click', new_func);

That sounds like an interesting alternative. *However, I'm finding
that I can't even access button B while I'm still inside button A's
onclick event handler because it was only just created by the previous
function and not yet accessible, so I think it is not possible this
way. *I need to somehow refresh the page before accessing button B.

Thanks for all of your great ideas, I appreciate them.
This is hard to make suggestions on without seeing your code, but are
you sure it's not accessible? Try assigning it to a variable during
creation, and then passing it to the function that needs to modify its
onclick?
Jun 27 '08 #9
On May 6, 9:06 pm, dunerunner <tpdi...@gmail.comwrote:
On May 5, 7:13 pm, apatheticagnostic <apatheticagnos...@gmail.com>
wrote:
What I have to begin with is button A only.
User clicks button A.
In the onclick event for button A I have a function that creates
button B. The function that creates button B attaches an onclick
event to button B. After button B is created (while still in the
onclick event handler) I need to add another function to button B's
onclick event.
Sorry if this isn't clear, but it's a strange requirement.
Thanks for trying to help.
If something like
buttonb.onclick = (function(original) {
return function() {
original();
new_function();
};
})(buttonb.onclick);
doesn't work, would using the level 2 DOM handlers work for you? i.e.
buttonb.addEventListener('click', new_func);

That sounds like an interesting alternative. However, I'm finding
that I can't even access button B while I'm still inside button A's
onclick event handler because it was only just created by the previous
function and not yet accessible, so I think it is not possible this
way. I need to somehow refresh the page before accessing button B.

Thanks for all of your great ideas, I appreciate them.
Presumably you kickoff the function that creates the button and adds
the onclick handler. That function should return a reference to the
button so you can add your onclick handler, but since you are using
getElementById and say you can't get a reference to it I'll presume
that doesn't happen.

The best solution is to have the function return a reference to the
button that it creates so that you can do stuff wtih it. But you say
you can't change that function so... a (rather horrible) alternative
is to use setTimeout with a short delay to add the onclick handler,
e.g.

function foo() {
...
addButtonB();
setTimeout( function(){
var buttonB = document.getElementById('buttonB');

/* add hanlder to buttonB using one of the
** methods suggested in this thread
*/

}, 10);
}

you may want to play with the length of the timeout, 10ms should be
OK.
--
Rob
Jun 27 '08 #10

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

Similar topics

4
by: Bharat Bhushan | last post by:
Hi, I need to generate random bytes for x number of times and keep appending it to a bigger byte array. How can I do this ? for (int lctr=0; lctr < main.Main.NoOfattributes(); lctr=lctr+1){ ...
4
by: bucket79 | last post by:
Hi is there anyway appending to dictionary? list has this feature >>>a = >>>a.append(1) >>>print a but dictionary can't
1
by: dmiller23462 | last post by:
Hey guys.... I put an error-handling in my page and have it posted at the complete end of the code, see below(when people were putting in 's I was getting the delimiter errors). Great, I...
7
by: Luc Tremblay | last post by:
Given the typical following code: void Listener::HandleEvent(const Event& event) { // handling code } In a "clean" fashion, how is it possible to add custom data (to be subsequently...
7
by: Shannan Casteel via AccessMonster.com | last post by:
I need some code to append several strings together, and insert those strings into a text box. But the insertion should only happen when a particular check box has been checked. For instance, I...
3
by: MLH | last post by:
I have a query, qryAppend30DayOld260ies that attempts to append records to tblCorrespondence. When run, it can result in any of the following: appending no records, appending 1 record or appending...
4
by: John A Grandy | last post by:
could someone explain the following to me : Appending the literal type character I to a literal forces it to the Integer data type. Appending the identifier type character % to any identifier...
1
by: Frank | last post by:
Hi, Let's say I have a file named myFile.xml Within that file I have blocks of data which I'd like to add at different times during the day. e.g. <LogEntry>
2
by: sarada purkait | last post by:
hii i have to write into a file from the start and then go on appending to it .. i tried using ( ios::out|ios::app) but by this the file keeps on appending every time i run the program and the...
1
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.