assign variable with id dynamically via jquery

Asked

Viewed 1,269 times

4

Hello What I need is this: have a form and on it I mount inputs whose values are the data of a table I run in a loop, I assign an id to each of these imputs with the field name and the number of an autoimcrement field in that table, then I want to take the values of these input via jquery picking these values by the id of each input and for that I wanted to pass the id of each record and in jquery mount the name of the input id dynamically into a variable, something like:

function salvaCamposForm(idDaTabela){
   var idInput1 = '#nomedoimput1'+idDaTabela;
   var idInput2 = '#nomedoimput2'+idDaTabela;
   // dai, passar isso para atribuir o valor para a variavel que
   // que vai ser passada para o post
   fPostInput1 = $(idInput1).val();
   fPostInput2 = $(idInput2).val();
   // ...
}

Can this be done in Jquery?

thanks!!!

Light and Peace!!!

  • How many inputs are there? or better: are all inputs within the form? what do you want to do at the end of the function, send ajax?

  • thank you friend! Next, has a form to register user with profphysiol experiences that is filled in a modal form, so far blz, but, when log in to the site, displays the full user form and within it I traverse the table experiences and assemble the inputs with the fields of this table in a div with a "Refresh Experience" button whose id is "_atliz_exp"+Idincretabela, so that when the guy clicks on one of these buttons I need to pass both the id of that button to the jquery to know which btn was clicked, and the id of the inputs in the divDess button and move to the post, as I wrote in the question.

  • I’m not sure if I understand, if you put the HTML it’s clearer... but having a $('button').on('click', fn... do you know which button is clicked right? your ID you can take with var id = this.id; and then you can use the $(this).closest('form'); to know the form or .closest('div') to find out what button the button is on... that’s what you want?

  • that is, both buttons and fields will have the same id followed by the number of the corresponding record, so I do not know the id’s neither buttons nor capos, so I need to do what I asked, i.e.: assemble the id text into a variable and take the value by this variable and pass the corresponding value to the post.

  • That is: exactly as I asked, as I do not know which boot nor which field, I need to do: var botaop = '#botaoppp"+idIncrement; var campop = '#campoxxx+idIncrement; dai fazer: $(botaop). on('click, fn... var postCampop = $(campoxxx). val(); ....

  • I don’t know if you got me right, take a look here: https://jsfiddle.net/z8pae6sx/ do you see that it is always possible to know the button and div ID? is that what you’re looking for? Join your HTML, it’s easier to understand the problem.

  • opa Rgio, it would look something like this but, in my case, I would even assign the id as in the case, var botaop = '#id_botao'+idDaTabela; and then $(botaop).on('click',fn...);

  • But you want to assign the ID when you click or the element already has an ID and you want to know what it is when you click?

  • that same, part of the id I know, which are fixed, the other part is the numer that has in the id field of each record, so, are several buttons, the guy clicks on one and I pass the id number of the record for Function, then I mount the variable with the fixed part of the id of fields plus the number coming in the Function parameter and pick the value using that variable that would have to be the id of the field, understood?

  • but thank you, I will see here what I do and put here the answer when finished, ok! values same guy

Show 5 more comments

1 answer

4

Thank you @Sergio!

your tip helped me a lot...
solved the problem using a part of it, there is the solution:

    $(document).ready(function(){
       $('button').on('click', function() {
           var id = this.id;
           // this id button is btnSlvExperiencia_2
           var reference = id.substr(19,2);
           saveExperiencia(reference);
        });
     });

function saveExperiencia(ref) {
    fexpId              = ref;
    femail              = $('#xemail').val();
    fcex_empresa        = $('#_cex_empresa_'+ref).val();
    fcex_data_inicio    = $('#_cex_data_inicio_'+ref).val();
    fcex_data_fim       = $('#_cex_data_fim_'+ref).val();
    fcex_funcao         = $('#_cex_funcao_'+ref).val();
    fcex_endereco       = $('#_cex_endereco_'+ref).val();
    // ...

$.post("config/slvExpCorr.php",{
    experiencia_id: fexpId,
    email: femail,
    cex_empresa: fcex_empresa,
    cex_data_inicio: fcex_data_inicio,
    cex_data_fim: fcex_data_fim,
    cex_funcao: fcex_funcao,
    cex_endereco: fcex_endereco
    // ...
    },
    function(data) {
        if(data > 0) {
           $("#titCorr").html("<b>Informações Pessoais Alteradas com Sucesso!</b>");
        } else {
            $("#titCorr").html("<b>Erro ao atualizar suas Informações Pessoais, verifique!</b>");
            $("#titCorr").css("color", "red");
        }
      }
    )
}

Therefore, I consider the issue solved
worked round

Light and Peace!

Browser other questions tagged

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