/    Sign up×
Community /Pin to ProfileBookmark

dynamic function with dynamic parameter on the right operand

hello everybody, i was trying for some days now to find a solution for this. had a look at different posts and forums online, but no luck so far…

is it possible to auto-create this with a loop???:

[CODE]
T$(‘infowindow1’).onclick = function(){ setInfobox(‘1’); }
T$(‘infowindow2’).onclick = function(){ setInfobox(‘2’); }
T$(‘infowindow3’).onclick = function(){ setInfobox(‘3’); }
T$(‘infowindow4’).onclick = function(){ setInfobox(‘4’); }
T$(‘infowindow5’).onclick = function(){ setInfobox(‘5’); }



[/CODE]

lets say something like:

[CODE]
id = 0;
for(id=1; id<=anzahl; id++){
T$(‘infowindow’+id).onclick = function(){ setInfobox(id); }
}
[/CODE]

tried EVAL on the right operand, tried WINDOW[] and many more…

any idea / help will be greatly appreciated!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERJul 01.2011 — ?

What kind of element does T$ represent? <div>, <span>, ???

What does the function setInfobox look like?

Questions are for purposes of testing my attempts at solution.
Copy linkTweet thisAlerts:
@deeKauthorJul 01.2011 — thanks for the reply and for trying!

T$ is a <span>

[CODE]
function setInfobox(id) {

resetInfoboxes();

T$('unterpunkte').getElements('span[class=red]').setStyle('display', 'block');

if(toggleID != id) { // EIN
T$('infowindow'+id).getElement('span[class=red]').setStyle('display', 'none');
T$('infowindow'+id).getElement('p[id=infotext'+id+']').setStyle('display', 'block');
T$('infowindow'+id).getElement('p[id=infotext'+id+']').setStyle('margin', '5px 0px 40px 0px');
toggleID = id;
} else { // AUS
T$('infowindow'+id).getElement('span[class=red]').setStyle('display', 'block');
toggleID = 'none';
}

}
[/CODE]
Copy linkTweet thisAlerts:
@rnd_meJul 01.2011 — [CODE]id = 0;
for(id=1; id<=anzahl; id++){(function(id){
T$('infowindow'+id).onclick = function(){ setInfobox(id); }
}(id))}[/CODE]
Copy linkTweet thisAlerts:
@JMRKERJul 01.2011 — 'rnd me' probably understands what you want better than me,

but this is my guess as to what you want to display.

See if you can modify this to your needs.
<i>
</i>&lt;!DOC HTML&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt; Untitled &lt;/title&gt;

&lt;style type="text/css"&gt;
.infowin { border:1px solid red; cursor:pointer; }
&lt;/style&gt;

&lt;script type="text/javascript"&gt;
// For: http://www.webdeveloper.com/forum/showthread.php?p=1161921#post1161921

window.onload = function() {
var obj = document.getElementById('SpanContainer');
var sel = obj.getElementsByTagName('span');
for (var i=0; i&lt;sel.length; i++) {
document.getElementById(sel[i].id).onclick = eventHandler.prototype.ShowAlert;
}
}

// Following from: http://forums.devx.com/archive/index.php/t-100007.html
function eventHandler() { // prototype instance
}
eventHandler.prototype.ShowAlert = function() {
alert(this.title); // alternative storage
// alert(this.innerHTML);
}

&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id="SpanContainer"&gt;
&lt;span id="infowindow0" class="infowin" title="Info 1"&gt; Span 1 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow1" class="infowin" title="Info 2"&gt; Span 2 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow2" class="infowin" title="Info 3"&gt; Span 3 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow3" class="infowin" title="Info 4"&gt; Span 4 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow4" class="infowin" title="Info 5"&gt; Span 5 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow5" class="infowin" title="Info 6"&gt; Span 6 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow6" class="infowin" title="Info 7"&gt; Span 7 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow7" class="infowin" title="Info 8"&gt; Span 8 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow8" class="infowin" title="Info 9"&gt; Span 9 &lt;/span&gt;&lt;p&gt;
&lt;span id="infowindow9" class="infowin" title="Info 10"&gt; Span 10 &lt;/span&gt;&lt;p&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@deeKauthorJul 02.2011 — guys thanks a lot!!!

indeed, rndme's solution was exactly what i was looking for.

before i had tried myself the first new "function" inside, but it wasnt working because i missed on " }(id))} " at the end... i still would like to understand how this works! what can i search for online, in order to find more info about it?
×

Success!

Help @deeK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...