Make sure `element` is correct in the context of displayed JS

closes #5293