I am trying to access html elements that I create in one js function in another function. I have this code
EDIT after comments: here is a jsfiddle http://jsfiddle.net/8uTxM/
</button>" +"<button value='1' type='button' class='as' id='c2' onclick='cA(this);'>"
in this function
function cA (element){
var x = element.value;
if (x === allQuestions[questionNumber].correctAnswer) {
element.setAttribute.style.backgroundColor = "green";
++score;
}
}
I am trying to make the button green when it is clicked. However, I get the error: Cannot set property 'backgroundColor' of undefined
I assume this has something to do with timing, but I cannot figure out why. Especially since the element.value bit works (the++score works fine, every right question adds +1 to the score variable)
onclick="functionName();"). Third, you could try passing thethisto your function and eliminating the need for separate functions altogether:onclick="setIndId(this);", and then passing it your functionfunction setIndId(element){ element.setAttribute("id",0+questionNumber)}. But it's very complicated to figure out like this, so please provide a fiddle or a reference to your code somewhere (if it is readable and not over complex)