How to gather & display an input value in the console (JS)

Hey guys!

I am working on the Wikipedia Viewer Project; the setup is done, now I am diving intp the tricky JS part haha; but before doing that, I want to make sure that I am able to retrieve all values that are typed into my input of type text.

I have tried selecting the element in JS and console.log(element.value), but the console logs “undefined”;
how can I achieve this?

Thank you :))

Try these


var value = $("#ElementID").val();

Plain JavaScript

var value = document.getElementById("ElementID");

I have tried the Jquery way, but as i click on the button the console still logs an empty string? what could be the reason?

Give code block in forum post .

.val function works for input elements not div okay …

var searchButton = document.querySelector(“button”);
var lucky = document.querySelector("#random");

searchButton.addEventListener(“mouseover”,function(){ = “#d7dde8”;
searchButton.addEventListener(“mouseout”,function(){ = “white”;
lucky.addEventListener(“mouseover”,function(){ = “#d7dde8”;
lucky.addEventListener(“mouseout”,function(){ = “white”;

var searchWord = $("#search input").val();


use it like this

oh my bad, did not remember I gave the same Id to div and input…thanks a lot, I’ll change Id and adjust !

1 Like