Shorten address of a group of elements with jQuery

Asked

Viewed 42 times

0

I got the following code:

$("div.conteudo div.administrador form.administradorEdita")

Which is a 

form

I want to refer to a label his;

var form = $("div.conteudo div.administrador form.administradorEdita");

How would it be?

form label#consulta 

OR

$("form label#consulta")?

OR

$("form').label#consulta")?

?

I would like to take for example the text of label

But not only that element label.

There are several. That’s one form.

That one form has several fields where each field gets its own style. So:

$("div.conteudo div.administrador form.administradorEdita")

It would be a shortening of address for the form.

I need to understand the dynamics of how to shorten this address.

See how giant it gets:

// JavaScript Document
$(document).ready(function(e) {

  $("div.conteudo div.administrador form.administradorCadastra").on("submit", function() {

      var tipo = $("div.conteudo div.administrador form.administradorCadastra select#tipo").val();
      var nome = $("div.conteudo div.administrador form.administradorCadastra input[type=text]#nome").val();
      var login = $("div.conteudo div.administrador form.administradorCadastra input[type=text]#login").val();
      var senha = $("div.conteudo div.administrador form.administradorCadastra input[type=password]#senha").val();

      if ( tipo == "" ||
           nome == "" || 
           login == "" || 
           senha == "") {

          alert("Algum campo está vazio!");

          return false;

       } 

         $("div.conteudo div.administrador form.administradorCadastra input[type=submit].btnAcesso").css('display', 'none');
         $("div.conteudo div.administrador form.administradorCadastra img").css('display', 'block');

         $.post ("../_requeridos/cadastraAdministrador.php", {

             tipo    : tipo,
             nome    : nome,
             login   : login,
             senha   : senha

         }, function(retorno){


             $("div.conteudo div.administrador form.administradorCadastra input[type=submit].btnAcesso").css('display', 'block');
             $("div.conteudo div.administrador form.administradorCadastra img").css('display', 'none');

              if (retorno == 1) {
                resposta = "Cadastrado com sucesso!";
              } else {
                resposta = "Erro no cadastro";
              }
             $(".resposta").css("display", "block");
             $(".resposta").html(resposta);     

           }
          );

          return false;

    });

});
  • Does the label have an id? If so, the @Diegomarques response calmly resolves.

  • Speak Carlos, I think the idea is this same, if you put an ID you do not need all of these paths. You search the element direct by the ID and ready...

  • So I thought of something like below to shorten the select#type". var type = $("this select#type"). val();

  • or var type = $(this) + $("select#type"). val();

1 answer

1

If the label has an ID, and the ID must be unique in an HTML document, you can get it straight from the ID. Example:

$("#consulta");

To get the text you can use the function "text()". Example:

$("#consulta").text();
  • So that’s the problem. It doesn’t just have this element. It’s several. But in each address the style will be different. So you can’t make $("#query"). text(); because in some places I will change the value of this field. I need to understand the dynamics of how to shorten this address.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.