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 8310259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:23:43+00:00 2026-06-08T19:23:43+00:00

I am positive I asked this question in the wrong way. I am attempting

  • 0

I am positive I asked this question in the wrong way. I am attempting to pass a string such as onClick="someFunc(arg1,arg2...)" to a javascript parser.

The parser should create an onClick event for someFunc with args (arg1, arg2 etc) .

My issue is that as the text is passed through the parser, the scope of the variable that holds the string name of someFunc is being passed down so that the attached events all register as the last declared string.

In the code below, attributes._onClick could be the string ‘onClick=”someFunc(arg1,arg2);” for one element followed by ‘onClick=”otherFunc(argA,argB);” for another element as the parser moves through the string.

The output in the browser (Firefox V14.01) has the event function always registering as otherFunc. So f_name in the closure is not evaluating as the variable but instead just registers as the last value of f_name declared throughout parsing the entire block.

I am not familiar with scope to the degree that I need to be to figure this out. Does anybody know a way I can attach the events in this manner (im trying to avoid going outside the parser to add the events) ?

           temp = attributes._onClick ;
            var x = 0 ;
            var f_name,f_args ;
            while(temp[x] != '') {
                temp[x] = temp[x].replace(')','').split('(') ;//remove paranthesis and separate func name from args
                f_name = temp[x][0] ;
                f_args = temp[x][1].split(',') ;//turn string of args into array
                el.onclick = function() { window[f_name].apply(el,f_args) ; }
                x++ ;
            }
  • 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-06-08T19:23:45+00:00Added an answer on June 8, 2026 at 7:23 pm

    You’re tripping over a pitfall in JavaScript that causes the same problem in various different ways to almost everybody at some point.

    The variable “f_name” is shared among all the functions you’re creating. That is, though each iteration of the loop creates a new function, there’s only one “f_name”. Thus, all functions see the same value in it, that being the value it had on the last iteration.

    There are several related solutions. One way is to wrap the statement that establishes the event handler in another anonymous function, one that you immediately invoke. That’ll allow you to create a copy of “f_name”, for exclusive use by one handler.

                   el.onclick = function(name) {
                    return function() { window[name].apply(el,f_args) ; };
                   }(f_name);
    

    Interestingly, there are probably hundreds of variations on this question here on SO.

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

Sidebar

Related Questions

i have asked this question before and it was closed because i was unable
I know this flavor of question has been asked multiple times, but I've spent
A while ago I asked this question: using xslt stylesheet to convert xhtml blank
Edit: I re-asked this same question (after fixing the problems noted with this question)
I was asked this interview question today!! (it was a really awkward telephonic interview..):
This question seems to have been asked before, but I feel like my situation
This question has been asked many times before, but I've found conflicting opinions on
So Python has positive and negative infinity: float(inf), float(-inf) This just seems like the
You can convert a negative number to positive like this: int myInt = System.Math.Abs(-5);
I found this question on an online forum: Really interested on how it can

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.