•
•
•
•
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 422,406 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 4,870 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
Views: 817 | Replies: 3
![]() |
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
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:
what i want to happen:
here is the sizechange function:
is there a way to do this (without using an html link)?
when i click the element SEND i want the RECEIVE element to receive the effect.
currently my script only effects the element itself:
javascript Syntax (Toggle Plain Text)
var animElements = document.getElementById("send").getElementsByTagName("p"); for(var i=0; i<animElements.length; i++) { animElements[i].onclick = sizeChange; }
what i want to happen:
javascript Syntax (Toggle Plain Text)
var animElements = document.getElementById("send").getElementsByTagName("p"); for(var i=0; i<animElements.length; i++) { when element is clicked, other element (recieve) goes to sizeChange }
here is the sizechange function:
javascript Syntax (Toggle Plain Text)
function sizeChange() { if (!this.currentWidth) this.currentWidth = 150; doWidthChangeMem(this,this.currentWidth,170,10,10,0.333); this.onclick = sizeRestore; } function sizeRestore() { doWidthChangeMem(this,this.currentWidth,150,10,10,0.5); this.onclick = sizeChange; }
is there a way to do this (without using an html link)?
•
•
•
•
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:
javascript Syntax (Toggle Plain Text)
var animElements = document.getElementById("send").getElementsByTagName("p"); for(var i=0; i<animElements.length; i++) { animElements[i].onclick = sizeChange; }
what i want to happen:
javascript Syntax (Toggle Plain Text)
var animElements = document.getElementById("send").getElementsByTagName("p"); for(var i=0; i<animElements.length; i++) { when element is clicked, other element (recieve) goes to sizeChange }
here is the sizechange function:
javascript Syntax (Toggle Plain Text)
function sizeChange() { if (!this.currentWidth) this.currentWidth = 150; doWidthChangeMem(this,this.currentWidth,170,10,10,0.333); this.onclick = sizeRestore; } function sizeRestore() { doWidthChangeMem(this,this.currentWidth,150,10,10,0.5); this.onclick = sizeChange; }
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:
javascript Syntax (Toggle Plain Text)
function sizeChange(el) { if (!el.currentWidth) el.currentWidth = 150; doWidthChangeMem(el,el.currentWidth,170,10,10,0.333); el.onclick = sizeRestore; }
Now you will have to use:
javascript Syntax (Toggle Plain Text)
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:
javascript Syntax (Toggle Plain Text)
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
•
•
Join Date: Feb 2008
Posts: 7
Reputation:
Rep Power: 0
Solved Threads: 0
i tried to implement the second solution you had, but it failed.
i will show you my html and my javascript, in case it might help.
and here is the javascript:
this did not work, so then i tried it withoput any editing, copying and pasting your code:
and that didn't work either. i am guessing it has to do with the way your code seems to call to the function, whereas the code that does work, is an identifier.
i will show you my html and my javascript, in case it might help.
html Syntax (Toggle Plain Text)
<div id="sender" class="examplediv"> <p>Click me!</p> <p>Click me!</p> </div> <div id="receiver" class="examplediv"> <p>look i expand!</p> <p>look i expand!</p> </div>
and here is the javascript:
javascript Syntax (Toggle Plain Text)
var sender = document.getElementById("sender").getElementsByTagName("p"); var receiver = document.getElementById("receiver").getElementsByTagName("p"); for(var i=0; i<animElements.length; i++) { sender[i].onclick = sizeChange() { sizeChange.call(reciever[i]); }; } function sizeChange() { if (!this.currentWidth) this.currentWidth = 150; doWidthChangeMem(this,this.currentWidth,170,10,10,0.333); this.onclick = sizeRestore; } function sizeRestore() { doWidthChangeMem(this,this.currentWidth,150,10,10,0.5); this.onclick = sizeChange; }
this did not work, so then i tried it withoput any editing, copying and pasting your code:
javascript Syntax (Toggle Plain Text)
var el1 = document.getElementById("sender").getElementsByTagName("p"); var el2 = document.getElementById("receiver").getElementsByTagName("p"); el1.onclick = function() { sizeChange.call(el2); }; function sizeChange() { if (!this.currentWidth) this.currentWidth = 150; doWidthChangeMem(this,this.currentWidth,170,10,10,0.333); this.onclick = sizeRestore; } function sizeRestore() { doWidthChangeMem(this,this.currentWidth,150,10,10,0.5); this.onclick = sizeChange; }
and that didn't work either. i am guessing it has to do with the way your code seems to call to the function, whereas the code that does work, is an identifier.
javascript Syntax (Toggle Plain Text)
var sender = document.getElementById("sender").getElementsByTagName("p"); var receiver = document.getElementById("receiver").getElementsByTagName("p"); for(var i=0; i<sender.length; i++) { sender[i].onclick = function() { sizeChange.call(reciever[i]); }; } function sizeChange() { if (!this.currentWidth) this.currentWidth = 150; doWidthChangeMem(this,this.currentWidth,170,10,10,0.333); this.onclick = sizeRestore; } function sizeRestore() { doWidthChangeMem(this,this.currentWidth,150,10,10,0.5); this.onclick = sizeChange; }
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!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- add onclick event programmatically (JavaScript / DHTML / AJAX)
- Replacing one object with another (JavaScript / DHTML / AJAX)
- Object reference not set to an instance of an object (ASP.NET)
- delphi object casting (Pascal and Delphi)
- Onactivate event not working for flash object in firefox (JavaScript / DHTML / AJAX)
- Javascript - Expected J and Expected Object errors (JavaScript / DHTML / AJAX)
- event object (HTML and CSS)
- object undefined instead of string literal (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: cookie
- Next Thread: calculating values using onclick


Linear Mode