Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #28699

Re: Is it possible to reassign js function to an object without including parameter(s)?

From Martin Honnen <mahotrash@yahoo.de>
Newsgroups comp.lang.javascript
Subject Re: Is it possible to reassign js function to an object without including parameter(s)?
Date 2015-09-20 16:42 +0200
Organization Liberty Development
Message-ID <mtmgko$fi9$1@news.albasani.net> (permalink)
References <40bb4feb-2cbf-4b11-86b5-4f845eee6ca8@googlegroups.com>

Show all headers | View raw


justaguy wrote:

> I have some js functions with one parameter and would like to re-assign a function to another object.  For instance,
> function f1(p1)
> {
> window.open('myscript.html?'+p1);
> }
>
> function f2(p2)
> {
> window.open('myscript2.html?'+p2);
> }
>
> The HTML file may read like this:
> <span id="me1" onclick="f1(p1)">stuff 1</span>
> <span id="me2" onclick="f2(p2)">stuff 2</span>

Where are the variables p1 and p2 used in the function calls f1(p1) and 
f2(p2) defined?
>
> Now, say, I want to reassign f2 to the object id of me1 on-the-fly and p2 value is unknown to me yet, simple reassignment:
> document.getElementById('me1').onclick = f2;  // won't work.

You can assign the function value f2 to the onclick handler. When the 
onclick is executed by the browser, the value of the the parameter p2 
would be undefined.

> and yet, I can't re-assign like this:
> document.getElementById('me1').onclick = f2(p2); // because p2 value is unknown yet.
>
> How to accomplish the above if doable? Or another way to tackle it? Thanks.

It is not clear where you expect the value of p2 to come from in any of 
your examples.

Note that <span id="me2" onclick="f2(p2)">stuff 2</span> sets the 
onclick handler to an anonymous function
   function(event) { f2(p2); }
where your p2 is not declared either, at least as long as you don't set 
up a global variable of the name p2 elsewhere.

Or do you have <span id="me2" onclick="f2('p2')">stuff 2</span>?

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Is it possible to reassign js function to an object without including parameter(s)? justaguy <lichunshen84@gmail.com> - 2015-09-20 07:06 -0700
  Re: Is it possible to reassign js function to an object without including parameter(s)? Martin Honnen <mahotrash@yahoo.de> - 2015-09-20 16:42 +0200
    Re: Is it possible to reassign js function to an object without including parameter(s)? justaguy <lichunshen84@gmail.com> - 2015-09-20 08:58 -0700
  Re: Is it possible to reassign js function to an object without including parameter(s)? danny <dann90038@gmail.com> - 2015-09-24 13:03 -0700
    Re: Is it possible to reassign js function to an object without including parameter(s)? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-25 09:11 +0200

csiph-web