<input type="text" class="search-bar" placeholder="Search">
<button onclick="show()">Show Your Text</button>
var x = document.getElementsByClassName("search-bar")[0].value;
function show(){
alert(x);
}
problem
in alert function the not show anything which i write in text box
The value which i write in textbox is show in alert box function
5
Answers
Try this
you need to retrieve the value of the input field inside the show() function.
You need to do two things to get this done:
<script>
tag.show()
function so that the alert text will display dynamically when clicking the button.You are assigning the value before the function is executed, that’s why it’s always blank.
try to declare variable "x" in the function only, because when you click on the button onClick event gets triggered and runs the show button, and inside the show function at that moment we have an undefined value.