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


Groups > comp.lang.javascript > #28698 > unrolled thread

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

Started byjustaguy <lichunshen84@gmail.com>
First post2015-09-20 07:06 -0700
Last post2015-09-25 09:11 +0200
Articles 5 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  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

#28698 — Is it possible to reassign js function to an object without including parameter(s)?

Fromjustaguy <lichunshen84@gmail.com>
Date2015-09-20 07:06 -0700
SubjectIs it possible to reassign js function to an object without including parameter(s)?
Message-ID<40bb4feb-2cbf-4b11-86b5-4f845eee6ca8@googlegroups.com>
Hi,

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>

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.
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. 

[toc] | [next] | [standalone]


#28699

FromMartin Honnen <mahotrash@yahoo.de>
Date2015-09-20 16:42 +0200
Message-ID<mtmgko$fi9$1@news.albasani.net>
In reply to#28698
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>?

[toc] | [prev] | [next] | [standalone]


#28700

Fromjustaguy <lichunshen84@gmail.com>
Date2015-09-20 08:58 -0700
Message-ID<ab8ca020-b024-47a6-9147-c28ad42c32c6@googlegroups.com>
In reply to#28699
On Sunday, September 20, 2015 at 10:42:41 AM UTC-4, Martin Honnen wrote:
> 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>?

Thanks for your useful response.  Since the p1 and p2 variable values are not available to the re-assignment I'm going with another approach now, and have just solved the problem.  This process helps a lot. fyi below.

Current State of Data View:
The HTML layout has left panel with id of "leftpanel" and right panel with id of "rightpanel".  
The "leftpanel" allows direct data entry and the "rightpanel" displays data.  The "leftpanel" is of width, 48% and the "rightpanel" is of width, 50%, between the two DIVs there's another div for medium of 2% for its width.

State of Data View in Development:
Now, I'd like to have an ability to enlarge the width of the "rightpanel" to 95% upon a click, it's easily done, once in that state, provide an ability (function) to revert back to the 48%, 2% and 50% layout of panels (default), also, easily done.

Current State of Data Editing:
In the default 48%, 2% and 50% layout of panels, upon click for editing a set of data is retrieved and displayed on the left panel (48% of width), once in that state, an icon to expand to maximize the editing screen.

Desired Future State of Data Editing:
Once the width of the "rightpanel" is enlarged to 95% upon a click,
support the current data editing functionality.

[toc] | [prev] | [next] | [standalone]


#28705

Fromdanny <dann90038@gmail.com>
Date2015-09-24 13:03 -0700
Message-ID<06826f8f-55f0-4e86-b031-c34087fc1b02@googlegroups.com>
In reply to#28698
to justaguy

you could simply, use the "arguments" collection to get whatever
 the 1st argument is, it doesn't have to be a parameter in the "arity",
 and give that to the event listener
 -----------------------------------------------

document.getElementById('me1').onclick = f2;
document.getElementById('me1').addEventListener("click", f2, false);

 function f2(){
		 window.open("myscript.html?"+arguments[0])
		 }

[toc] | [prev] | [next] | [standalone]


#28706

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-09-25 09:11 +0200
Message-ID<XnsA5205D7CE7F3Ceejj99@194.109.6.166>
In reply to#28705
danny <dann90038@gmail.com> wrote on 24 Sep 2015 in comp.lang.javascript:

> to justaguy
> 
> you could simply, use the "arguments" collection to get whatever
>  the 1st argument is, it doesn't have to be a parameter in the "arity",
>  and give that to the event listener
>  -----------------------------------------------
> 
> document.getElementById('me1').onclick = f2;
> document.getElementById('me1').addEventListener("click", f2, false);

Both?

>  function f2(){
>            window.open("myscript.html?"+arguments[0])
>            }

arguments[0] will convert to an empty string every time,
when clicking 'me1', I expect.



-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web