Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    String + rand syntax

    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

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Have you tried something like this:
    Code:
    <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>
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    zeldaze (05-07-2008)

  • #3
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ahh, don't know why I didn't think of a while loop :P

    Thanks a lot. Final code:

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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •