0

Following my code:

var a;
var b = new Array('t1');
for(var i =0; i < b.length; i++){
    document.getElementById('test').innerHTML='<div onclick="a = i;">a</div>';
}

The error is: "i" is not defined. How to solve it?

1
  • 1
    Just to make sure: you realise that the i and a variables that you stick in that innerHTML string are not the variables i and a but just letters in a string, right? That aside, when do you get that error? When it runs, or when you click on the resulting link? (because if you try the latter, it will fairly obviously break when it tries to execute a=i in global context. Also: don't do this, use function calls) Commented Nov 17, 2013 at 20:49

1 Answer 1

2

Should work...

var a;
var b = new Array('t1');
for(var i =0; i < b.length; i++){
    document.getElementById('test').innerHTML='<div onclick="a = '+i+';">a</div>';
}
Sign up to request clarification or add additional context in comments.

Comments

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.