+ 1

Why cant access the variable in javascript InnerHTML in this code

When I do like this , got a error, "can't access lexical declaration 'a' before initialization " Where can i do define the variable https://code.sololearn.com/Wa11a19A9a4a/?ref=app

23rd May 2021, 11:32 AM
ShamsuCm
2 Respostas
+ 4
// The right order of statements and a few little things should be as follows: function work(){ let name let saveBtn let table let a name = document.getElementById('name') saveBtn = document.getElementById('save_btn') table = document.getElementById('table') saveBtn.addEventListener('click',()=>{ if (name.value === '') { alert ('please fill in the form ') return false }else{ a = 'nice name' } table.innerHTML += `<tr><td>${name.value}</td><td>${a}</td> </tr>` }) } document.addEventListener('DOMContentLoaded', work)
23rd May 2021, 12:11 PM
JaScript
JaScript - avatar
+ 1
ShamsuCm You have declared variable after accessing it so move your this line let a above this line var table = document.getElementById('table') or change let a to var a ----------------- shift your if else condition above this line var table = document.getElementById('table') because you first need to validate your input fields then get and set data in table https://code.sololearn.com/WBBUgCwqwmCD/?ref=app
23rd May 2021, 11:57 AM
A͢J
A͢J - avatar