1

I need some help getting and "onclick" event onto some links in a couple drupal pages.

I've got 2 pages, each with a text entry field set to "full HTML", within which I've added my a tags. the links work great in this format, however when I try to add my onclick event (which contains jquery info for analytics tracking), the only thing I see when I inspect element is the text "onclick" and nothing after it. Not even the " =" ", which would suggest a script error.

When that didn't work I added a class to the A tags and tried to use jquery to append the onclick attribute with these jquery functions: setAttribute attr click

While it sounds simple none of what I've tried has worked (I had used both setAttribute and click successfully on other links), so now I'm looking for a new method. the module Menu attributes doesn't work since it doesn't deal with "onclick" and my links are not menu items, they are just links in text.

I'm using Drupal 6.19 and jquery update.

If anyone has advice or suggestions, please let me know,

Thanks Steph

5
  • jQuery doesn't have a setAttribute() function, you're probably thinking of the basic javascript function of that name. Since you just want to bind to an event you don't need to inline an attribute, just use $('.selector').click(function() { doSomething(); });
    – Clive
    Commented Jul 17, 2013 at 15:10
  • Hi Clive, Thanks for replying, I've tried the click function you mentioned without success (though I have gotten it to work on different areas of the site, just not here), can you suggest anything else?
    – StephanieF
    Commented Jul 17, 2013 at 15:12
  • Not really, sorry. That's definitely how to attach an event in jQuery 1.3 though; beyond that it's not really a Drupal issue so I doubt we'll be able to help here
    – Clive
    Commented Jul 17, 2013 at 15:22
  • At least that rules out stuff, thanks for the help!
    – StephanieF
    Commented Jul 17, 2013 at 15:36
  • Can you post your anchor tag and jQuery? Commented Jul 17, 2013 at 17:23

2 Answers 2

0

Sorry all, I discovered my question was invalid. I was able to user onclick directly in the "full html" text area, and while the value of the attribute didn't show in the view source, it was working.

I'm not sure if D6 always handles onclick in full html text areas or not. I choose to believe there is something causing this in my theme.

Thanks for your attention!! Steph

0

An alternative approach is to apply a class to your links, either manually or by adding an option to your WYSIWYG editor (eg. a custom style in CKEditor or a custom button in BUEditor) and then put your onclick javascript in a file in your theme or a custom module.

This approach is preferable to putting the JS code in the text because it means your code is in one place (in the single js file instead of scattered across potentially multiple nodes), the code is not stored in the database (the linked page mentions PHP but most of it also applies to javascript), and you aren't allowing users unrestricted HTML input, which is a potential security risk.

As for the code, Clive's jQuery example from the comments above would work well in this case:

$('.selector').click(function() {
  doSomething();
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.