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

142
Views
Jquery form element access

I'm new to jquery and trying to do the following:

I have a form:

<form method="POST" class="add-product" >
...
<label name="message"></label>
...
</form>

And script:

$(document).ready(function() {
$(".add-product").submit(function(e) {
    e.preventDefault();
    var form = this;
    $.ajax({
        type: "POST",
        url: "/product/add/",
        data: $(this).serialize(),
        success: function(data) {
            $(form.elements["message"]).html(data.message);
        }
    });
});

});

I'm trying to update label with message, but it doesn't work. Seems that I have some mistake in syntax:

$(form.elements["message"]).html(data.message);
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

The issue is because the label does not appear in the form.elements collection. Instead you need to select it directly:

$(".add-product").submit(function(e) {
  e.preventDefault();
  var form = this;

  // inside the AJAX callback...
  var data = {
    message: 'Foo bar'
  }
  $(form).find('label[name="message"]').html(data.message);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form method="POST" class="add-product">
  <label name="message"></label>

  <button>Submit</button>
</form>

about 4 years ago · Santiago Trujillo Report

0

Try using the selector:

$("form.add-product label[name='message']").html(data.message)

You can read more about jQuery Attribute selector's here. https://api.jquery.com/attribute-equals-selector/

If you have more than one label on the page with the attribute name='message' then the above won't work.

about 4 years ago · Santiago Trujillo Report

0

You used wrong jquery selector, try it:

$('label[name="message"]').html(data.message);

hope this help;

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!