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 401,964 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 2,969 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: 674
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 5
Solved Threads: 39
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Help Re: onclick changes other object

  #2  
Mar 1st, 2008
Originally Posted by adrumsolo4u View Post
what i am trying to accomplish is to have two elements lets say SEND and RECEIVE.
when i click the element SEND i want the RECEIVE element to receive the effect.

currently my script only effects the element itself:
  1. var animElements = document.getElementById("send").getElementsByTagName("p");
  2. for(var i=0; i<animElements.length; i++) {
  3. animElements[i].onclick = sizeChange;
  4. }

what i want to happen:

  1. var animElements = document.getElementById("send").getElementsByTagName("p");
  2. for(var i=0; i<animElements.length; i++) {
  3. when element is clicked, other element (recieve) goes to sizeChange
  4. }

here is the sizechange function:
  1. function sizeChange() {
  2. if (!this.currentWidth) this.currentWidth = 150;
  3. doWidthChangeMem(this,this.currentWidth,170,10,10,0.333);
  4. this.onclick = sizeRestore;
  5. }
  6. function sizeRestore() {
  7. doWidthChangeMem(this,this.currentWidth,150,10,10,0.5);
  8. this.onclick = sizeChange;
  9. }

is there a way to do this (without using an html link)?


The sizeChange and sizeRestore functions reference the HTML Element calling them via the this keyword. When the onclick event is is triggered, this will refer to the Element on which it is triggered.

The simplest (to understand) thing to do is to change all the this keyword to a variable such as el. And pass the Element you want changed to the function as el.

eg:

  1. function sizeChange(el) {
  2. if (!el.currentWidth) el.currentWidth = 150;
  3. doWidthChangeMem(el,el.currentWidth,170,10,10,0.333);
  4. el.onclick = sizeRestore;
  5. }

Now you will have to use:
  1. el1.onclick = function() { sizeChange(el2); };

where el1 is the element to be clicked, and el2 is the element to change.

Another way of doing it is to have the function called in the scope of the element to be changed, so that this would refer to the correct element.

eg:

  1. el1.onclick = function() { sizeChange.call(el2); };
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 6:30 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC