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

218
Views
My button doesn't send her form but another form

I have 2 forms in my html code, but when I go to press the button of form2, the value of form1 is sent

  function chk() {
    var text = document.getElementById('text').value;
    console.log(text);

    return false;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="form1">
  <input type="text" id="text" value="Hi">
  <input type="submit" id="submit1" onclick="return chk()">
  <label for="submit1">Hi</label>
</form>

<form id="form2">
  <input type="text" id="text" value="bonjour">
  <button id="submit2" onclick="return chk()">btn</button>
  <label for="submit2">bonjour</label>
</form>

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

0

Ids should be unique on a page. You can use a name for the input elements instead.

function chk(formId) {
  var text = document.getElementById(formId).querySelector('input[name=text]').value;
  console.log(text);
  return false;
}
<form id="form1">
  <input type="text" name="text" value="Hi">
  <input type="submit" id="submit1" onclick="return chk('form1')">
  <label for="submit1">Hi</label>
</form>
<form id="form2">
  <input type="text" name="text" value="bonjour">
  <button id="submit2" onclick="return chk('form2')">btn</button>
  <label for="submit2">bonjour</label>
</form>

about 4 years ago · Juan Pablo Isaza Report

0

the id "text" is used twice in your code, which can lead to confusion, you should try changing it to "text1" and "text2" And use the one you need in your function.

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!