How to avoid the execution of a function at the time of defining.

M

Moses

Hi All,

How to avoid the execution of a function at the time of
defining.

Here i am giving the details.


I am creating the following div container through DOM.

<div id="content">
<a href="#" onclick="displayDiv('content')" > Click</a>
</div>

The Code is

var category_list = document.getElementById('category_list');
var dom_div = document.createElement('div');
dom_div.id = 'content';
var dom_link = document.createElement('a');
dom_link.href ='#';
dom_link.onclick = displayDiv();
val = document.createTextNode('Click');
dom_link.appendChild(val);
dom_div.appendChild(dom_link);
category_list.appendChild(dom_div);


The displayDiv Funcion


function displayDiv()
{
dv = document.getElementById('content'); //Here is the error.
}



The Problem is when the following script
dom_link.onclick = displayDiv('content');
is executed it is calling the function displayDiv(name)
Here we have the code
document.getElementById('content');
which throws the error.

The reason is the div container is not yet created.

What I need is the function should be called only on the click event .
It should not be called while I define it to the Click Event. (ie it
should not be called at the time of defining)
How to achieve this.

Thanks in Advance


Regards
Moses
 
R

RobG

Hi All,

How to avoid the execution of a function at the time of
defining.

Here i am giving the details.

I am creating the following div container through DOM.

<div id="content">
<a href="#" onclick="displayDiv('content')" > Click</a>
</div>

The Code is

var category_list = document.getElementById('category_list');
var dom_div = document.createElement('div');
dom_div.id = 'content';
var dom_link = document.createElement('a');
dom_link.href ='#';
dom_link.onclick = displayDiv();

That will execute the function immediately and assign the result, you
want to assign a reference so remove the call operator:

dom_link.onclick = displayDiv;
 
M

Moses

Hi Rob,

Thanks for your reply,

dom_link.onclick = displayDiv;

If I need to pass parameters what to do?


Regards
Moses
 
S

scripts.contact

Hi Rob,

Thanks for your reply,

dom_link.onclick = displayDiv;

If I need to pass parameters what to do?

dom_link.onclick = function(){ displayDiv(para1,para2) }
or
dom_link.onclick = 'displayDiv(para1,para2)'
 
R

RobG

Hi Rob,

Thanks for your reply,

dom_link.onclick = displayDiv;

If I need to pass parameters what to do?


dom_link.onclick = function() {
displayDiv(parm1, parm2);
}


However, if the parameters are local variables of the outer function,
you will be creating closures. That usually isn't an issue but may
have unexpected or unwanted effects (or not).

To avoid closures, use (wrapped for posting):

dom_link.onclick = new Function(
'displayDiv(' + parm1 + ', ' + parm2 + ')');
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top