Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Alternative to 'onclick' for a variable number of links?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jeff Dunlap  
View profile  
 More options Nov 11 2012, 9:51 am
From: Jeff Dunlap <jeff_j_dun...@yahoo.com>
Date: Sun, 11 Nov 2012 06:51:14 -0800 (PST)
Local: Sun, Nov 11 2012 9:51 am
Subject: Alternative to 'onclick' for a variable number of links?

I'm learning MooTools and converted some javascript into a MooTools class.

Please note that the number of hyperlinks generated by the server are
completely variable.  Although the following appears to work fine is there
a better way than using 'onclick'?

<a href="" onclick="myclass.myfunc(param1,param2); return
false;">some_text</a>


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Arian Stolwijk  
View profile  
 More options Nov 11 2012, 9:57 am
From: Arian Stolwijk <stolwijk.ar...@gmail.com>
Date: Sun, 11 Nov 2012 15:57:03 +0100
Local: Sun, Nov 11 2012 9:57 am
Subject: Re: [Moo] Alternative to 'onclick' for a variable number of links?

Yes, inline event handlers are in almost all cases a bad idea.

With MooTools you can use .addEvent.

so you have this html:

<a href="#" id="link">some text</a>

and this JS

$('link').addEvent('click', function(event){
    event.preventDefault(); // prevents the default action (which is the
"return false" in your code")
    myclass.myfunc(....);

});

If you have more links you can use

$$('some css selector').addEvent('click', ...);

Or sometimes better:

<div id="parent">
    <a href="#>link 1</a>
    <a href="#>link 1</a>
    <a href="#>link 1</a>
    <a href="#>link 1</a>
</div>

$('parent').addEvent('click:relay(a)', function(event){
    // ...

});

On Sun, Nov 11, 2012 at 3:51 PM, Jeff Dunlap <jeff_j_dun...@yahoo.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Dunlap  
View profile  
 More options Nov 11 2012, 11:21 pm
From: Jeff Dunlap <jeff_j_dun...@yahoo.com>
Date: Sun, 11 Nov 2012 20:21:46 -0800 (PST)
Local: Sun, Nov 11 2012 11:21 pm
Subject: Re: [Moo] Alternative to 'onclick' for a variable number of links?

Arian,

Thank you for responding.  Your suggestions gave me a good starting point
for me to avoid inline event handlers, i.e.:
onclick="myclass.myfunc(param1,param2);

This is what I have so far:

http://jsfiddle.net/jeff_j_dunlap/Dfa5h/

Although I am able to access the parameters, storing them in the
hyperlink's href, it does not feel right.  Am I approaching this the wrong
way?

Thanks again


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sanford Whiteman  
View profile  
 More options Nov 11 2012, 11:56 pm
From: Sanford Whiteman <sa...@figureone.com>
Date: Sun, 11 Nov 2012 23:55:58 -0500
Local: Sun, Nov 11 2012 11:55 pm
Subject: Re: [Moo] Alternative to 'onclick' for a variable number of links?

> Am I approaching this the wrong way?

If you're using eval(), it's *definitely* the wrong way.

See the touchups I made here:

    http://jsfiddle.net/Dfa5h/1/

· you can chain addEvent right on the result of $$(). $$() returns an
array of Elements, and you can run any Element method, such as
addEvent() on Elements as well, and it will map each item in the array
to the method

· use data-* attributes and you don't need to pollute the href
attribute, which should be useable in scriptless environments or at
the very least give a nice display in the status bar

· simple split() instead of eval -- eval must be destroyed

-- Sandy


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Dunlap  
View profile  
 More options Nov 12 2012, 12:45 am
From: Jeff Dunlap <jeff_j_dun...@yahoo.com>
Date: Sun, 11 Nov 2012 21:45:14 -0800 (PST)
Local: Mon, Nov 12 2012 12:45 am
Subject: Re: [Moo] Alternative to 'onclick' for a variable number of links?

Sandy, that's exactly what I needed!  Thank you!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Rolf Langenhuijzen  
View profile  
 More options Nov 12 2012, 3:38 pm
From: Rolf Langenhuijzen <plentyofr...@gmail.com>
Date: Mon, 12 Nov 2012 12:38:06 -0800 (PST)
Local: Mon, Nov 12 2012 3:38 pm
Subject: Re: [Moo] Alternative to 'onclick' for a variable number of links?

gotcha... $$(eval).destroy();


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »