JS will only execute once your PHP script has been parsed and the resultant HTML sent to the browser.
As such, a one to one translation is not possible.
However, if you could tell us what you are trying to do, then I’m sure a JS solution could be found.
I want to program the code above in Javascript, so I want Javascript to do what the code above does. Instead of doing the above in PHP I want to do it in Javascript.
Well, the thing is, you won’t be able to translate that snippet one for one, drop it in your page and have it work.
That is because PHP is parsed and the resultant HTML is sent to your browser. JavaScript then operates on the HTML that the browser receives.
That’s not to say that what you are trying to achieve with PHP cannot be done with JS, but your snippet provides nowhere near enough context.
For example what do the variables $HTML_Ouput, $answers[$qid][$aid] and $avalue contain?
The best way for us to help you is if you describe what you are trying to do and provide some HTML and possibly JS for us to work with.
Also, a link to a page where we can see everything would be handy.
What the variables $HTML_Ouput, $answers[$qid][$aid] and $avalue contain are not relevant for the problem. You should assume that in Javascript these variables contain the same info.