Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

145
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

You used wrong jquery selector, try it:

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

hope this help;

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda