...

View Full Version : how do i concactinate a identifier?



Inoob
12-15-2006, 10:04 AM
firstly this is what I have..


<script type="text/javascript">
function mouseOver(txt)
{
if (txt == "b1")
document.b1.src ="images/rollbutton.gif"
if (txt == "b2")
document.b2.src ="images/rollbutton.gif"
if (txt == "b3")
document.b3.src ="images/rollbutton.gif"
if (txt == "b4")
document.b4.src ="images/rollbutton.gif"
if (txt == "b5")
document.b5.src ="images/rollbutton.gif"
if (txt == "b6")
document.b6.src ="images/rollbutton.gif"
if (txt == "b7")
document.b7.src ="images/rollbutton.gif"
if (txt == "b8")
document.b8.src ="images/rollbutton.gif"
if (txt == "b9")
document.b9.src ="images/rollbutton.gif"
if (txt == "b10")
document.b10.src ="images/rollbutton.gif"
}
function mouseOut(txt)
{
if (txt == "b1")
document.b1.src ="images/b1.PNG"
if (txt == "b2")
document.b2.src ="images/b2.PNG"
if (txt == "b3")
document.b3.src ="images/b3.PNG"
if (txt == "b4")
document.b4.src ="images/b4.PNG"
if (txt == "b5")
document.b5.src ="images/b5.PNG"
if (txt == "b6")
document.b6.src ="images/b6.PNG"
if (txt == "b7")
document.b7.src ="images/b7.PNG"
if (txt == "b8")
document.b8.src ="images/b8.PNG"
if (txt == "b9")
document.b9.src ="images/b9.PNG"
if (txt == "b10")
document.b10.src ="images/b10.PNG"
}
</script>

what I am trying to make it is...


function mouseOver(txt) document.+txt+.src ="images/"+txt+".PNG"
function mouseOut(txt) document.+txt+.src ="images/rollbutton.gif"

I mean you see what I want just I have no idea how to pull it off, I just want it to do it by itself to eliminate redundancy, redundancy hurts my eyes :(

Kor
12-15-2006, 10:11 AM
I presume that 'txt' passes the value of a name attribute, right?



function mouseOver(txt){
document.getElementsByName(txt)[0].src ='images/rollbutton.gif';
}
function mouseOut(txt){
document.getElementsByName(txt)[0].src ='images/'+txt+'.png';
}


But I advise you to use rather id's instead of names, and getElementById(id) instead of getElementsByName(name)[0].

Inoob
12-15-2006, 10:24 AM
well I almost thought it was impossible, takes geniouses like you to prove me wrong :D :) Thank you kor! Inoob~~ :) dis is awesome redundancy NO MORE! :D my background is in mIRC, I'm jumping into php and javascript, and in mIRC I learned redundancy is retarded, and that lesson follows me everywhere, and now redundancy that I create makes me kinda mad at myself coz I know i cud do better, thanks for all your help kor, you made my day :D (well morning... 4:24AM here :P)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum