Now I have a new problem. When the onclick event is added with the
standard event model (addEventListener via jQuery) I can't seem to get
access to it.

I noticed the event jQuery added to the element is stored in a
property called 'click' instead of the standard 'onclick'. So I tried
something like this:

<script type="text/javascript">
    $(document).ready(function() {
        $('#clickTester').click(function(){
            alert('dynamic onclick!')
        });
        $('#clickTester').each(function() {
            var onClickStd = this.onclick;
            var onClickDyn = this.click;
            $
(this).unbind('click').removeAttr('onclick').click(function(){
                if (confirm('Perform Original Action?')) {
                    if ($.isFunction(onClickStd)) {
                        onClickStd();
                    }
                    if ($.isFunction(onClickDyn)) {
                       onClickDyn();
                    }
                }
            });
        });
    });
</script>
<input type="button" id="clickTester" onclick="alert('Original Action
Performed.');" value="Do It!">

Which works find except, I get this error:
[Exception... "Illegal operation on WrappedNative prototype object"
nsresult: "0x8057000c (NS_ERROR_XPC_BAD_OP_ON_WN_PROTO)" location: "JS
frame :: http://localhost/tests/onclickPrepend.html :: anonymous ::
line 20" data: no] http://localhost/tests/onclickPrepend.html Line 20

What is that?

Mark

On Jan 31, 12:02 pm, Mark <[EMAIL PROTECTED]> wrote:
> Ok, well I have a solution! I guess IE doesn't like the eval()
> function.http://ajaxian.com/archives/evaling-with-ies-windowexecscript
>
> <script type="text/javascript">
> var global = this;
>     $(document).ready(function() {
>         var onClickAttr = $('#clickTester').attr('onclick');
>         $('#clickTester').removeAttr('onclick').click(function(){
>             if (confirm('Perform Original Action?')) {
>                 if (window.execScript) {
>                     window.execScript('(' + onClickAttr.toString() + ')
> ();');
>                 }
>                 else {
>                     (global.eval) ? global.eval(onClickAttr) :
> eval(onClickAttr);
>                 }
>             }
>         });
>     });
> </script>
> <input type="button" id="clickTester" onclick="alert('Original Action
> Performed.');" value="Do It!">
>
> On Jan 30, 3:11 pm, "Mark T" <[EMAIL PROTECTED]> wrote:
>
> > I am trying to make the on-click event of any element optional depending on
> > what the user decides. The only functions I see out there append a function
> > to the on-click event. I have played with the browser bubbling / catching
> > stuff too. That worked in Firefox but not in IE 7. It seems I can't access
> > the on-click function if it was defined within the element's onclick=""
> > attribute.
>
> > Here are my two approaches. Tell me if you have a better idea.
>
> > <b>1) eval() </b>// I guess IE doesn't like us using this. Firefox doesn't
> > care.
> > <script type="text/javascript">
> >     $(document).ready(function() {
> >         var onClickAttr = $('#clickTester').attr('onclick');
> >         $('#clickTester').removeAttr('onclick').click(function(){
> >             if (confirm('Perform Original Action?')) {
> >                 eval(onClickAttr);
> >             }
> >         }
> >     });
> > </script>
> > <input type="button" id="clickTester" onclick="alert('Original Action
> > Performed.');" value="Do It!">
>
> > <b>2) Bubbling </b>// Again, IE problems while Firefox works just fine.
> > <script type="text/javascript">
> >     $(document).ready(function() {
> >         var $span = $('&lt;span&gt;&lt;/span&gt;').attr('onclick',
> > $('#clickTester').attr('onclick'));
> >         $('#clickTester').removeAttr('onclick').wrap($span);
> >         $('#clickTester').click(function(event){
> >             if (!confirm('Perform Original Action?')) {
> >                 event.stopPropagation();
> >             }
> >         });
> >     });
> > </script>
> > <input type="button" id="clickTester" onclick="alert('Original Action
> > Performed.');" value="Do It!">

Reply via email to