'In console I m getting null [duplicate]

I trying to access value from the input tag but in console it showing me undefined or null


       <div class="searchbar">
             <input type="text" id="tags" placeholder="Search for a state...">
       </div>
             button type="button" id="add-btn-data" onclick="addstate()">add</button>
var inputstate = document.getElementById('tags').value;
function addstate(){
    console.log(inputstate);
}


Solution 1:[1]

Try to declare variable inside the function it might work

function addstate(){
var inputstate = document.getElementById('tags').value;
console.log(inputstate);

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sadow