Event problems

S

suzanne.boyle

This has been driving me round the bend for hours and I'm not sure
what I should be googleing for to look for a solution so please help!

When you click a div I want to show another div, then when you either
click on either the first div or the newly shown one it hides it
again. I think I'm on the right track, this is my code...

function openDiv(ddl, options) {
this.ddl_obj = document.getElementById(ddl);
this.options_obj = document.getElementById(options);

this.ddl_obj.onclick = dropdownMenu.prototype.click;
this.options_obj.onclick = dropdownMenu.prototype.click;
}

openDiv.prototype.click = function() {
alert(this);
if ( this.options_obj.style.display == 'none' )
this.options_obj.style.display = 'block';
else
this.options_obj.style.display = 'none';
return true;
};

and at the bottom of the page...
obj = new openDiv('div1','div2');

The problem is when the event is being handled, this in the handler
points to the html object that fired it. I want it to point to the
openDiv object. Am I anywhere near to a solution. As you can
probably tell, javascript is not my best subject.
 
G

Geoffrey Summerhayes

This has been driving me round the bend for hours and I'm not sure
what I should be googleing for to look for a solution so please help!

When you click a div I want to show another div, then when you either
click on either the first div or the newly shown one it hides it
again. I think I'm on the right track, this is my code...

function openDiv(ddl, options) {
this.ddl_obj = document.getElementById(ddl);
this.options_obj = document.getElementById(options);

this.ddl_obj.onclick = dropdownMenu.prototype.click;
this.options_obj.onclick = dropdownMenu.prototype.click;

}

openDiv.prototype.click = function() {
alert(this);
if ( this.options_obj.style.display == 'none' )
this.options_obj.style.display = 'block';
else
this.options_obj.style.display = 'none';
return true;

};

and at the bottom of the page...
obj = new openDiv('div1','div2');

The problem is when the event is being handled, this in the handler
points to the html object that fired it. I want it to point to the
openDiv object. Am I anywhere near to a solution. As you can
probably tell, javascript is not my best subject.

Use closures.
In OpenDiv, for example(untested):

this.ddl_obj.onclick = function() {this.click()};
 
S

suzanne.boyle

Use closures.
In OpenDiv, for example(untested):

this.ddl_obj.onclick = function() {this.click()};

I did a quick test of this and it gives a 'this.click is not a
function' error. I'll have another play about with it and look into
closures tomorrow, unfortunately I haven't had time today.
 
G

Geoffrey Summerhayes

I did a quick test of this and it gives a 'this.click is not a
function' error. I'll have another play about with it and look into
closures tomorrow, unfortunately I haven't had time today.- Hide quoted text -

My bad.

this.ddl_obj.onclick = function(e){return function(){e.click()}}
(this);
 
S

suzanne.boyle

My bad.

this.ddl_obj.onclick = function(e){return function(){e.click()}}
(this);

Thanks, that works a treat. Now I just need to read up on closures to
find out why.
 

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

No members online now.

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,420
Latest member
ZitaVos505

Latest Threads

Top