1

I'm doing this

mcomp7d101.onRelease = function() {
    getURL("javascript:Compartir("+id7d101+");");
}

mcomp7d102.onRelease = function() {
    getURL("javascript:Compartir("+id7d101+");");
}

mcomp7d103.onRelease = function() {
    getURL("javascript:Compartir("+id7d101+");");
}

mcomp7d150.onRelease = function() {
    getURL("javascript:Compartir("+id7d101+");");
}

you get the idea :)

How can I use a for loop to do something like:

for(ii = 101; ii < 150; ii++)
{
    mcomp7d+ii.onRelease = function() {
    getURL("javascript:Compartir("+id7d+ii);");
    }
}

I'm getting a syntax error. It seems that I can't create variable variables in compiled languages.

2
  • 2
    i hate sintax-- so regressive. Commented Aug 31, 2012 at 18:46
  • Unexpected economics joke on stack! Got me thinking anyway - are you suggesting the poor have a monopoly on sin? Commented Sep 1, 2012 at 17:09

1 Answer 1

2

You can use the following syntax:

for(ii = 101; ii < 150; ii++) 
{ 
    this["mcomp7d" + ii].onRelease = function() 
    { 
        getURL("javascript:Compartir(" + this['id7d' + ii] + ");"); 
    } 
}
Sign up to request clarification or add additional context in comments.

2 Comments

Guess, you not totally correct: getURL("javascript:Compartir('id7d" + ii + "');"); this will pass string to "Compartir", but he needs to pass value of id7d101 variable to all functions. Leave like this: getURL("javascript:Compartir(" + id7d101 + ");");
Thanks for the correction radistao. I think it would probably be this then getURL("javascript:Compartir(" + this['id7d' + ii] + ");");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.