[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

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

knowledgenotebook

9/20/2015 2:06:00 PM

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.

4 Answers

Martin Honnen

9/20/2015 2:43:00 PM

0

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

knowledgenotebook

9/20/2015 3:58:00 PM

0

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.


Danny

9/24/2015 8:03:00 PM

0

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])
}

Evertjan.

9/25/2015 7:11:00 AM

0

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)