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

206
Views
Set a value from selector input

I have a form with many input

$("input[name='firstname']")

Return

Object { 0: input.form-control, 1: input.form-control, 2: input.form-control, 3: input.form-control, length: 4, prevObject: {…} }

I search to set a value to the input, so I tried

$("input[name='firstname']")[0].val=12;
$("input[name='firstname']")[0].text=12;

I don't see the value in the input.

So I tried

$("input[name='firstname']")[0].val("12");

Uncaught TypeError: $(...)[0].val is not a function

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The jQuery function $(selector) returns a jQuery object which acts somewhat like a collection of native elements.

When accessing indexed properties like [0], it returns the native HTMLElement.

If you want to just access the first matched element within the object but still have it be a jQuery object, use .eq(). The .val() method lets you safely set the value.

$("input[name=firstname]").eq(0).val(12)

Alternately, don't use jQuery at all

// querySelector returns the first match or null
const firstName = document.querySelector("input[name=firstname]");
if (firstName) {
  firstName.value = 12;
}
about 4 years ago · Santiago Trujillo 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!