Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6667863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:58:42+00:00 2026-05-26T02:58:42+00:00

I’m dealing with a situation where I need to bind jQuery events to a

  • 0

I’m dealing with a situation where I need to bind jQuery events to a page to handle UI updates that is being generated via JSF. Alas, JSF sucks and it sticks onclick events on everything, which pre-empt any of my jQuery work.

Thanks to the fine folks here on SO, I found a solution for that:

mixing my jQuery click events with existing object's onclick attribute

The logic is:

  • on page load, grab all of the onclick attributes and store them in a variable.
  • bind my jQuery events
  • after my own jQuery events, I can then eval the original onclick: eval(onclickValueVariable)

This worked find in all my dummy onclick event testing.

But then it failed on that live JSF page. The issue is that all of the onclick’s end with a ‘return false’ which leads to this error:

return not in function

For example:

<div class="uglyJSFcreatedTag" onclick="console.log('hey');return false">

And the jQuery that would fire it:

var $jsfTag = $('.uglyJSFcreatedTag');
var cacheOnclick = $jsfTag.attr(onclick);
$jsfTag.removeAttr('onclick');

...bunch of logic here to decide if the inline onclick should fire and, if so...

eval(cacheOnclick)

The problem is the return false. It looks like nothing can be returned when firing a eval.

Is there a solution for this? I imagine I could take the value as a string and do string parsing to remove all return false, then call that from the script itself. But that sounds a bit messy. Is there a more elegant solution?

If not, what particular JS statements should I be looking for to filter out before calling eval?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:58:42+00:00Added an answer on May 26, 2026 at 2:58 am

    Two options:

    1. Using your current approach, just wrap the onclick string with a function before you eval it. Store the result, and call it as a function. Make sure you call it with the appropriate this context:

    var f = eval("(function(){ "+cacheOnclick+"})");
    f.call($jsfTag[0]);
    

    Note: the parenthesis () around the function declaration, are required within the eval. This makes the declaration into an expression (syntactically speaking), thus making it legal in in the eval.

    2. instead of grabbing the onclick attribute, grab the actual function from the dom element itself. Also, unless you need to do something special with the jsf handler function from your code, I would suggest that you just add the JSF function as a jquery click handler directly, rather than calling it explicitly from your code:

    var $jsfTag = $('.uglyJSFcreatedTag');
    $jsfTag.bind('click', $jsfTag[0].onclick);
    $jsfTag.removeAttr('onclick');
    

    Personally, I would go for approach #2, but either one should work.


    Update: Here’s a litte additional explanation for Option #2:

    var $jsfTag = $('.uglyJSFcreatedTag');
    

    That’s from your example — we’re using jquery to retrieve a set containing all elements with the classname ‘uglyJSFcreatedTag’.

    $jsfTag.bind('click', $jsfTag[0].onclick);
    

    This fetches the first element from the set ($jsfTag[0]), and gets the onclick property of that element object. .onclick is a javascript property that contains a reference to the compiled function that the browser generated from the “onclick” attribute’s string content. Now, since we have the handler function, we can bind it directly to the jquery click event, using the jquery bind() function.

    $jsfTag.removeAttr('onclick');
    

    Finally, we just remove the attribute. This is ok, because we’ve already bound the function via jquery. If we don’t remove it from the “old-style” onclick, then it’ll get called twice.

    Note: You may have noticed that the above code only works on the first element in the selected set. For the very likely case that your set contains multiple elements, you’ll want to loop through the set and handle each element separately. Here’s how you would do that:

    var $jsfTag = $('.uglyJSFcreatedTag');
    $jsfTag.each( function(idx, element) {
        $(element).bind('click', element.onclick).removeAttr("onclick");
    });
    

    If you want to skip the jsf handler for certain elements, then insert logic within the “each loop” to test for this, and skip the call to bind accordingly.


    If you must continue to call the jsf handler from within your click handler, then at least consider using element.onclick instead of $(element).attr('onclick'). The latter requires eval, while the former does not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.