473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Event problems

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.

Jun 10 '07 #1
4 1407
On Jun 10, 3:45 pm, suzanne.bo...@gmail.com wrote:
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()};

---
Geoff

Jun 10 '07 #2
On 10 Jun, 21:43, Geoffrey Summerhayes <sumr...@gmail.comwrote:
On Jun 10, 3:45 pm, suzanne.bo...@gmail.com wrote:
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()};

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

Jun 11 '07 #3
On Jun 11, 6:15 pm, suzanne.bo...@gmail.com wrote:
On 10 Jun, 21:43, Geoffrey Summerhayes <sumr...@gmail.comwrote:
Use closures.
In OpenDiv, for example(untested):
this.ddl_obj.onclick = function() {this.click()};
---
Geoff

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);

--
Geoff
Jun 12 '07 #4
On 12 Jun, 01:01, Geoffrey Summerhayes <sumr...@gmail.comwrote:
On Jun 11, 6:15 pm, suzanne.bo...@gmail.com wrote:
On 10 Jun, 21:43, Geoffrey Summerhayes <sumr...@gmail.comwrote:
Use closures.
In OpenDiv, for example(untested):
this.ddl_obj.onclick = function() {this.click()};
---
Geoff
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);

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

Jun 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bob | last post by:
Below is sample code that illustrates what I'm trying to do. For sake of brevity I didn't include the properties of buildBtn that determine what data to request. The problem is I never see...
1
by: Andreas | last post by:
Hi, we have some problems with the following situation: A remote application A writes files to a directory monitored by a FileSystemWatcher-instance in application B. A creates the file with...
6
by: Michael Johnson Jr. | last post by:
I am trying to handle a button click event, which updates a web control table with data. The button is dynamically created in the table itself. When I call updateTable() in the Page_Load the new...
5
by: Paul Bromley | last post by:
Can someone give me a very simple example on how to do this? As an example I have a commaned button in a user control. Once this user control is placed on a form I want to be able to respond in the...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
1
by: mxliron | last post by:
hi all, this code is for a picture viewer. yes, this is my first code and my first piece of programming so i maybe simply be missing some key notions here. the problem is: the nav onclick event...
3
by: steve | last post by:
Hi all, I want to be able to hirearchally define event delegate (declarations) WITHIN interfaces. Unfortunatelly C# 1 doesn't appear to support it. How does the following look as a...
2
by: Robert | last post by:
Hello javascript group readers, I have a question regarding how to prevent memory leaks in Internet Explorer when using closures. I already knew about the circular reference problem, and until...
14
by: Snor | last post by:
I'm attempting to create a lobby & game server for a multiplayer game, and have hit a problem early on with the server design. I am stuck between using a threaded server, and using an event driven...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.