Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
How can I get the input of a button on a website using Javascript?

I'm a bloody beginner at this,so please don't judge me,ok? So I made a Website with this text field:

Text: <input type="text" name="text" value="" id="input1"/>

And I made a button,used to get the input of the Text field:

<input type="submit" onclick=getinput>

and this is the getinput()-function:

<script>
   function getinput(){
     const val = document.querySelector('input1').value;
     console.log(val);
     console.log("No error in function implement.");
   }
</script>

but I generated the following error:

VM195:3 Uncaught TypeError: Cannot read properties of null (reading 'value')
    at <anonymous>:3:45

I don't know what this error means,and how to fix it,could someone please help me?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You have some small mistake / bugs in your code. i refeactor it.

1.) the querySelector was wrong. For Ids use '#slectorName'.

2.) onclick="getinput()" instead onclick=getinput

function getinput(){
    const val = document.querySelector('#input1').value;
    console.log(val);
    console.log("No error in function implement.");
}
<input type="text" name="text" value="" id="input1"/>

<input type="submit" onclick="getinput()">

about 4 years ago · Juan Pablo Isaza Report

0

The problem is with the selector. To select an id with document.querySelector, you have to prefix it with a hashtag. Like:

function getinput(){
  const val = document.querySelector('#input1').value;
  console.log(val);
  console.log("No error in function implement.");
}

Alternatively, you can use document.getElementById. For example:

function getinput(){
  const val = document.getElementById('input1').value;
  console.log(val);
  console.log("No error in function implement.");
}

Also, in the submit button, you have to wrap the attribute value in double quotes. You have to write this:

<input type="submit" onclick="getinput()">

instead of this:

<input type="submit" onclick=getinput>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!