...

View Full Version : String + rand syntax



zeldaze
05-07-2008, 10:39 AM
Hi guys,

I just need to see if I have this right. I am fairly new to Javascript etc. and I don't know it's syntax but this is what I need done. I have a function that checks if and element in my HTML coding has a certain ID and if it does it applies effects to it.
For example:
function('note1');

So if I have something say <span id="note1">, then this function checks if the ID = note1 and then does its stuff. However I have more than one of these so if I have <span id="note2"> etc. in one page then only the variables that are entered will be effected.

I could write a long list inside the function consisting of "note1", "note2" etc all the way up to 999, but instead I would like to know the syntax for making something like this

function('note*');

So it will be any id beginning with note regardless of the number that is after it. I have tried the above but it doesn't work. I am not sure what this feature is called but I just need a headsup.

Thanks :)

rangana
05-07-2008, 10:59 AM
Have you tried something like this:


<script type="text/javascript">
var i=1;
window.onload = function()
{
while(i<=999)
{
document.getElementById('note'+i).onclick=function()
{
alert(this.id); //Just for test, place whatever here
}
i++
}
}
</script>

zeldaze
05-07-2008, 11:03 AM
Ahh, don't know why I didn't think of a while loop :P

Thanks a lot. Final code:


function init(){
var i=1;
while(i<=999)
{
makeEditable('note'+i);
i++
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum