473,498 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trouble adding an OnClick Dynamically.

Hey everyone, i am doing some stuff where i'd like to pop up a little
confirmation before a user clicks on a 'delete' link. Just trying to
keep the markup clean, i added an attribute pre_act="confirm" to all
of the links that i wanted to do this to and am trying to add an
onclick event that will cancel the click action if they fail. I've got
a test page at http://randomwebspace.net/testing/testpage.html. It has
no unintentional XHTML validation errors, and i am sure that i'm
finding the right elements ((i'm also turning their backgrounds red,
just to be sure))...but the event doesn't get attached. Does anyone
see what my issue is?

Basically the markup that i want to affect looks like:
<a class="iconlink" href="http://www.google.com"
pre_act="confirm">delete</a>

And the code that i'm trying ((for this iteration)) to add an onclick
event is:
function fnConfirm(){ return false; return(confirm('Are you sure?')); }
hecConfirmable[i].onClick = fnConfirm; /*
HtmlElementCollection[i]...called from a loop */

I'm running the loops that call that code once the body has loaded so i
don't think it's an issue of the DOM not being available yet.

Any ideas? Thanks!

-Brendan

May 2 '06 #1
9 2184
May be you can try this...

<a href="some link" onclick="return func();">SomeText</a>

and in the function, func() show the prompt and return true or false as
the functions return value.

-Rafiq
http://www.ajaxtoday.com

May 2 '06 #2
ajax wrote:
May be you can try this...

<a href="some link" onclick="return func();">SomeText</a>
[snip]


Yes, i certainly could do that and i don't have any particular reason
not to other than i'd like to avoid it if i can. I'd like to add the
onclick dynamically so as to keep javascript out of my markup - does
anyone know how i would work that?

May 2 '06 #3
Donius said the following on 5/2/2006 2:57 PM:
Hey everyone, i am doing some stuff where i'd like to pop up a little
confirmation before a user clicks on a 'delete' link. Just trying to
keep the markup clean, i added an attribute pre_act="confirm" to all
of the links that i wanted to do this to and am trying to add an
onclick event that will cancel the click action if they fail. I've got
a test page at http://randomwebspace.net/testing/testpage.html.


And you want someone to wade through 625+ lines of JS code to try to
figure out the problem???

Your standard.js has a lot of problems in it though.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 2 '06 #4
Donius said on 03/05/2006 6:46 AM AEST:
ajax wrote:
May be you can try this...

<a href="some link" onclick="return func();">SomeText</a>
[snip]

Yes, i certainly could do that and i don't have any particular reason
not to other than i'd like to avoid it if i can. I'd like to add the
onclick dynamically so as to keep javascript out of my markup - does
anyone know how i would work that?


It's always good to start with valid markup:
<URL:http://validator.w3.org/check?uri=http%3A%2F%2Frandomwebspace.net%2Ftestin g%2Ftestpage.html>

There is no particular benefit to keeping JavaScript out of your markup
when the alternative is adding invalid (X)HTML attributes. I can't see
that it's better to rely on the client executing a series of functions
that perform conditional compilation and browser sniffing, plus search
through every element in the document looking for some to modify, when
the onclick handler could have been added at the server.

How much difference is there between:

<a ... pre_act="confirm" ...>

and

<a ... onclick="return fnConfirm(this);" ...>

from a 'clean code' perspective? Either the additional attribute is
added at the server or at the client. In either case, the HTML author
has to decide whether to add a particular attribute & value or not.
Your server code could use the pre_act attribute and replace it with the
onclick handler.

Also consider if you use the load event to attach the handlers and a
user clicks on one of your buttons before the page is fully loaded, the
onclick handler won't have been added. If page loading is slow, users
will often press Esc several times to cancel loading then try to use the
page as-is - that will stop onload from occurring.

Anyhow, your issues may be cured by fixing fnConfirmations():

hecConfirmable[i].onClick = fnConfirm;
---------------------^^^

JavaScript is case sensitive, as is XHTML. The property you seek is
onclick.

--
Rob
Group FAQ: <URL:http://www.jibbering.com/FAQ>
"It is much easier for a web developer to create a site
that receives no complaints than to create a site that
doesn't warrant any." --Richard Cornford
May 2 '06 #5
On 02/05/2006 19:57, Donius wrote:

[snip]
hecConfirmable[i].onClick = fnConfirm;


Property names are case-sensitive. The intrinsic event properties are
all lower case (onclick, in this case).

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
May 3 '06 #6
Responding to everything all at once:

Randy Webb wrote:
And you want someone to wade through 625+ lines of JS code to try to
figure out the problem???
No man! If it was in my standard.js i would have found it - that code
goes into a good number of sites.
Your standard.js has a lot of problems in it though.
Such as? I know, i browser sniff. For Mac IE 5, which has crashed on
half the JS i've ever used. I'm fine with that. Are there other
problems that you see? I wouldn't mind critique... I can't see much
wrong with it, but if you have anything to mention in that 625+ line
page, i'd be glad to hear it. :) I'm not even doing anything crazy
like modifying prototypes, which is one of my favorite things *ever*!

RobG wrote: It's always good to start with valid markup
I know, i checked the markup...i came back with invalid element
attribute errors (intentional) and some errors due to my using
javascript without enclosing it CDATA sections or however else i'm
supposed to hide javascript from the validator. Is there another way
to include javascripts in-page that works with this? I suppose i could
put them all in external files...
How much difference is there between:

<a ... pre_act="confirm" ...>

and

<a ... onclick="return fnConfirm(this);" ...>
Not much at all! Like i said, i'm doing it this way more or less
because i'd like to. I'm transforming from XML for a good deal of it
and there's no reason i can't use an onclick, but the data is being
used for other things, where 'pre_act="confirm"' also has some similar
meaning, so it seemed most ubiquitous to use that.
Also consider if you use the load event to attach the handlers and a
user clicks on one of your buttons before the page is fully loaded, the
onclick handler won't have been added. If page loading is slow, users
will often press Esc several times to cancel loading then try to use the
page as-is - that will stop onload from occurring.
A very good point, I will take that into consideration.
Anyhow, your issues may be cured by fixing fnConfirmations():

hecConfirmable[i].onClick = fnConfirm;
---------------------^^^

JavaScript is case sensitive, as is XHTML. The property you seek is
onclick.


My editor autofilled it to onClick! Damned dirty thing! Thank you to
both RobG and Mr Winter for pointing that out. :) I appreciate it.

Thanks guys,
-Brendan

May 3 '06 #7
Donius said the following on 5/3/2006 8:30 AM:
Responding to everything all at once:

Randy Webb wrote:
And you want someone to wade through 625+ lines of JS code to try to
figure out the problem???
No man! If it was in my standard.js i would have found it - that code
goes into a good number of sites.


It wasn't the case here, but sometimes code in one .js file can cause
problems in another .js file or in the page itself. var conflicts and
the such.
Your standard.js has a lot of problems in it though.


Such as? I know, i browser sniff. For Mac IE 5, which has crashed on
half the JS i've ever used. I'm fine with that.


That was one of them that I recall.
Are there other problems that you see?


The major one I remember was the document.getElementById emulation for
document.all browsers. I remember that one most because I am, to the
best of my knowledge, the first person to post that code here in c.l.j:

if(document.all && !document.getElementById) {
document.getElementById = function(id) { return document.all[id]; }
}

It works, sometimes, properly in IE4 but it doesn't always do what it is
supposed to do.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 5 '06 #8
JRS: In article <_d******************************@comcast.com>, dated
Thu, 4 May 2006 22:41:00 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.com> posted :

The major one I remember was the document.getElementById emulation for
document.all browsers. I remember that one most because I am, to the
best of my knowledge, the first person to post that code here in c.l.j:

if(document.all && !document.getElementById) {
document.getElementById = function(id) { return document.all[id]; }
}

It works, sometimes, properly in IE4 but it doesn't always do what it is
supposed to do.


Now that's an unreasonable statement, because you have no way of telling
what the person who puts (or reads) it on a Web page supposes that it
will do.

If it is supposed that it can be used in any browser to ensure that
document.getElementById will be available with full customary
semantics, then AIUI it will not always do that.

But if it is supposed that, where a page contains <div ID=XYZ>..</div>
and there is no other XYZ in sight, the constructed getElementById can
be used to get a reference enabling the contents of the div to be
changed, then AIUI it will work.

It is, AFAIK, a partial emulation of the full version, and reliable
within the limits which its author should have expected.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
May 6 '06 #9
Dr John Stockton said the following on 5/6/2006 11:32 AM:
JRS: In article <_d******************************@comcast.com>, dated
Thu, 4 May 2006 22:41:00 remote, seen in news:comp.lang.javascript,
Randy Webb <Hi************@aol.com> posted :
The major one I remember was the document.getElementById emulation for
document.all browsers. I remember that one most because I am, to the
best of my knowledge, the first person to post that code here in c.l.j:

if(document.all && !document.getElementById) {
document.getElementById = function(id) { return document.all[id]; }
}

It works, sometimes, properly in IE4 but it doesn't always do what it is
supposed to do.
Now that's an unreasonable statement,


No, it's not John.
because you have no way of telling what the person who puts (or reads)
it on a Web page supposes that it will do.
While that is pedantically true, I know what the original intended
purpose of it was, as presented to me.

The emulation of getElementById in IE4.

<URL: http://www.metalusions.com/backstage/articles/8/ >

Read it yourself. And, anybody who understands the above code gets the
first impression that it is - gasp - attempting to emulate gEBI for IE4.

If it is supposed that it can be used in any browser to ensure that
document.getElementById will be available with full customary
semantics, then AIUI it will not always do that.
And hence my statement that it does not do what it is supposed to do.
Maybe rewording the statement can satisfy the pedantics:

It doesn't do what it was originally written to do.
But if it is supposed that, where a page contains <div ID=XYZ>..</div>
and there is no other XYZ in sight, the constructed getElementById can
be used to get a reference enabling the contents of the div to be
changed, then AIUI it will work.
And only if the browser supports document.all, doesn't support gEBI,
supports dynamically changing an element, and so on...
It is, AFAIK, a partial emulation of the full version, and reliable
within the limits which its author should have expected.


No, it doesn't. Read the website.

<quote>
The following bit of script checks to see if document.all is present,
but document.getElementById is not. If this is the case it then assigns
a new function to document.getElementById that wraps around document.all

if(document.all && !document.getElementById) {
document.getElementById = function(id) {
return document.all[id];
}
}

With this script present at the top of the head section, either in an
external file or an inline script, all that is needed to reference an
element on IE, Mozilla/NS6.x and a number of other browsers is
document.getElementById('theID')
</quote>

And that run on sentence at the end is false.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
May 6 '06 #10

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

Similar topics

4
44906
by: Michael Hill | last post by:
I had this html: <tr id="action" title="click to do something" onclick="alert('mike');"> <td>a</td> <td>b</td> </tr> and it works when I click on the row, but when I try to add it...
3
3124
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the button is clicked. However, that table to which a...
2
18540
by: RobG | last post by:
I am trying to dynamically add an onclick to an element, however I just can't get the syntax right. consider the following function: function doClick (evt,x) { // do things with evt and x } ...
10
6533
by: stephen.cunliffe | last post by:
Hi, Okay, here's my problem... I'm dynamically building some content (similar to this): var fooObj = document.createElement('div'); fooObj.setAttribute('id', 'uniqueNumber'); etc.
3
4854
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
0
2386
by: Luis Esteban Valencia | last post by:
Hello I wrote a program with code behind in C# to add row into table dynamically and the program worked very well in .Net Framework 1.1. When I run this program in .Net Framework 2.0 beta...
5
8485
by: Mark Rae | last post by:
Hi, Is it possible to add events, specifically a Click event, to a dynamically created TableCell? I have an <asp:Table...> control to which I'm dynamically adding TableRow controls made up of...
11
11159
by: Daz | last post by:
Hello everyone. I am sure the answer to my question is simple, but I can't seem to dynamically add an onClick event to my script. I have a table which is generated dynamically, I am just...
5
13299
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
7125
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7004
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
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
5464
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3095
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3085
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1423
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
657
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
292
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.