Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Mar 12, 2010 15:00:56 GMT -8
Is it possible to have an array variable as a function? My whole idea was to make an a forloop array to save space from each onclick function which i was doing. But as i thought would happen the function could not read the function name.
I made an example of it basically, not what im doing, but its the same format.
<p onclick="tab1(this)" > words </p> <div id="disp1">more words</div>
<script> var tabsi=new Array(); tabsi[0]=['tab1(editme)','disp1']; tabsi[1]=['tab2','disp2']; tabsi[2]=['tab3','disp3'];
function tabsi[0][0] { editme.innerHTML="New Words" document.getElementById(tabsi[0][1]).innerHTML="yada" } </script>
I wanted to get that tab1(editme) in a for loop so i can make one function and change needed variables with tabsi[x][1], or tabsi[x][2], etc..
But I cant get it to work the way i wanted to.
I looked at having it go at this..
<p onclick="tab1(this,b,c)" > words </p> <div id="disp1">more words</div>
<script>
function tab1(editme,disp1,c) { editme.innerHTML="New Words" document.getElementById(disp1).innerHTML="yada" } </script>
But i dont know how to set that up, with multiple variables in the function. and how to designate those variables (a,b,c) to what i want. so if it needs to be done this way(if it can work like this?), uh, can you lead me on the right path? please? lol sry if im buggin you with all of my questions. -.- I'm learning it on my own, with just w3schools, and w/e google search engine brings up. lol So, i get alot of questions. -.- I should start posting on PBS. lol.
|
|
|
Post by Wormopolis on Mar 13, 2010 2:37:43 GMT -8
your post kinda confused me...
you want an array of functions?
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Mar 13, 2010 11:35:09 GMT -8
uh like i have
4 onclicks and 4 different divs <p onclick="tab1(this)" > words </p> <div id="disp1">more words</div> <p onclick="tab2(this)" > Words </p> <div id="disp2">more words</div> <p onclick="tab3(this)" > wOrds </p> <div id="disp3">more words</div> <p onclick="tab4(this)" > woRds </p> <div id="disp4">more words</div>
And for each one of these onclicks, i have a function for it.
<script> function tab1(editme) { editme.innerHTML="New Words" document.getElementById('disp1').innerHTML="yada" }
function tab2(editme) { editme.innerHTML="New WOrds" document.getElementById('disp2').innerHTML="yada" }
function tab3(editme) { editme.innerHTML="New WoRds" document.getElementById('disp3').innerHTML="yada" }
function tab4(editme) { editme.innerHTML="New WorDs" document.getElementById('disp4').innerHTML="yada" } </script>
And i wanted to combind all of the functions into one function, and it will still work. And it would also work if all i did was add a 'tab5(this)' 'disp5' to it. thus looping though the array idea.
so the end result will still have those 4 onclicks and 4 div's but 1 fucntion that will have a easy edit if you add another 'onclick'.
Hope this helps?
Edit: i did take another look at it, and instead of making the function part of the array, i made each onclick the same
<p onclick="tab(this)" > words </p> <div id="disp1">more words</div> <p onclick="tab(this)" > Huka </p> <div id="disp2">more words</div> <p onclick="tab(this)" > Buck </p> <div id="disp3">more words</div>
and made an array out of the tab name
<script> function tab(editme) { var tabsi=new Array(); tabsi[0]=['words','disp1']; tabsi[1]=['Huka','disp2']; tabsi[2]=['Buck','disp3'];
for (u=0; u<tabsi.length; u++) { if (editme.innerHTML.match(tabsi[0])) { document.getElementById(tabsi[1]).innerHTML="yada" } </script>
So now all you have to do is place in the tab name, and id of the div. So , i guess i did get it to work. but could it have been taken a different way from what i was explaining above? more or less how would of you done it?
And now as i look at my script again, it could mess up if someone names a tab like 'lol' and another like 'lolwop' cause the .match(lol) would go and enable that part (if after), not the lolwop. -.- ugh, lets try for innerHTML==
sry lol, im ranting.
|
|
|
Post by Wormopolis on Mar 13, 2010 17:33:19 GMT -8
what if you had it check the id of the div following the <p>?
<p onclick="tabber(this, this.nextSibling)" > woRds </p> <div id="disp4">more words</div>
function tabber(editme, targ) { var changeArray=[ ['disp1','changing contents of div1'], ['disp2','derka derka'], ['disp3','homina homina homina'], ['disp4','and we can just keep adding into this array...'] //no comma on last one ];
for (i=0; i<changeArray.length; i++) { if (targ.id==changeArray[0]) { targ.innerHTML=changeArray[1]; editme.innerHTML="words and stuff"; } }
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Mar 13, 2010 21:33:20 GMT -8
Yeah, it would work. except if we were doing it by what my use for it is, not the example, the layout would be somewhat like this. <table> <tr> <td onclick="tabbs(this)">Tab1</td><td onclick="tabbs(this)">Tab2</td><td onclick="tabbs(this)">Tab3</td></tr><tr><td id="info_plc"> <div id="disp1">Wordy</div><div id="disp2">letters</div><div id="disp3">spaces</div> </td> </tr> </table> Which will exclude that nextsibling. I guess it would be easier to answer if i just showed you what im doing, instead of giving random examples that relate to what im doing. topekat3.proboards.com/index.cgiIts the table at the top with the 4 tabs. infact can this line be altered like this? <p onclick="tabber(this, this.nextSibling.nextSibling.nextSibling)" > woRds </p> <p onclick="tabber(this, this.nextSibling.nextSibling)" > woRds </p> <p onclick="tabber(this, this.nextSibling)" > woRds </p> <div id="check">Mate</div> Same function different variable depending on which one is hit?
|
|
|
Post by Wormopolis on Mar 14, 2010 0:30:14 GMT -8
well if you are going to target the same div the entire time, you could just
<p onclick="tabber(this, document.getElementById('check'));" >words </p>
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Mar 14, 2010 5:59:40 GMT -8
o, huh. i didnt know you could set a variable like that, in a function.
so if i had more than one div, i can just make it like this?
<p onclick="tabber(this, document.getElementById('checky'))" > woRds </p> <p onclick="tabber(this, document.getElementById('checker'))" > woRds </p> <p onclick="tabber(this, document.getElementById('checks'))" > woRds </p>
<div id="checks">Mate</div> <div id="checky">Mate</div> <div id="checker">Mate</div>
function tabber(editme, targ) { //assuming all divs are already at display="none" targ.style.display="" editme.bgColor="blue" }
|
|
|
Post by Wormopolis on Mar 15, 2010 1:15:36 GMT -8
yep!
|
|
Topeka
Junior Member
Posts: 94
Topeka said 0 great things
|
Post by Topeka on Mar 15, 2010 17:05:08 GMT -8
sweeeeet!
Thank you very much.
|
|
|
Post by Wormopolis on Mar 17, 2010 16:40:30 GMT -8
|
|