User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 423,248 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,215 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting

onclick changes other object

Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: onclick changes other object

  #4  
Mar 2nd, 2008
  1. var sender = document.getElementById("sender").getElementsByTagName("p");
  2. var receiver = document.getElementById("receiver").getElementsByTagName("p");
  3. for(var i=0; i<sender.length; i++) {
  4. sender[i].onclick = function() { sizeChange.call(reciever[i]); };
  5. }
  6. function sizeChange() {
  7. if (!this.currentWidth) this.currentWidth = 150;
  8. doWidthChangeMem(this,this.currentWidth,170,10,10,0.333);
  9. this.onclick = sizeRestore;
  10. }
  11. function sizeRestore() {
  12. doWidthChangeMem(this,this.currentWidth,150,10,10,0.5);
  13. this.onclick = sizeChange;
  14. }

this is closer to what you want.

that change I made was:

for(var i=0; i<sender.length; i++) {
	sender[i].onclick = function() { sizeChange.call(reciever[i]); };
	}

Now the only thing you have to do, is make sure reciever[i] is available when sender[i].onclick is triggered. As it is, i will be equal to sender.length when any of the sender[i].onclick s triggered. (your loop would have completed when i = sender.length)

Making sure reciever[i] is availalbe requires that you either 'persist' it using a javascript closure, or you can save a reference to reciever[i] somewhere where you can access it when the onclick event occurs.
It depends on which you are comfortable with.

Using a reference: here we save a reference to reciever[i] to the actual htmlElement sender[i].

for(var i=0; i<sender.length; i++) {
	sender[i]._receiver = reciever[i];
	sender[i].onclick = function() { sizeChange.call(this._receiver); };
	}

some ppl prefer a closure.

for(var i=0; i<sender.length; i++) {
	sender[i]._receiver = reciever[i];
	sender[i].onclick = function() {
var _receiver = reciever[i];
return function() { sizeChange.call(_receiver); };
}(reciever[i]);
	}

this is a bit more advanced method of persisting object references or scope in javascript. You can search google for "javascript closure" or "javascript curry" on the topic.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
All times are GMT -4. The time now is 9:46 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC