Fetch table value and put in form

Asked

Viewed 46 times

2

This is the table has the fields the goal is to click the edit button and while clicking the instruction jquery that I put on top will fill in the fields automatically referring to the line that was clicked

Here’s the thing I want to take the table records and put them in a modal input text and I used the next code (It’s working but only for <input type="text"> but for textarea and combobox doesn’t work and that’s what I want)

<script>
    $('body').on("click", ".edit", function() {
        $('#alterar_ID').val($(this).parents('tr').find('td').eq(0).text());
        $('#alterar_si').val($(this).parents('tr').find('td').eq(0).text());
        $('#alterar_nome').val($(this).parents('tr').find('td').eq(1).text());
        $('#alterar_data').val($(this).parents('tr').find('td').eq(2).text());
        $('#alterar_duracao').val($(this).parents('tr').find('td').eq(3).text());
        $('#alterar_tipo').val($(this).parents('tr').find('td').eq(4).text());
        $('#alterar_relatorio').val($(this).parents('tr').find('td').eq(5).text());

    });
</script>

That is, by clicking a button in the table it will fetch the values corresponding to the indexes and put them in the modal inputs. My question is as follows and if I want to put in a textarea or leave a combobox?

  • For both the same way works, so much so that if you do $('#alterar_nome').val() You will get the value of this field. You can use the .va() normally

  • could post html code or your doubt MCVE?

  • is to place the combobox with the string equal to that of the table or in the case of the client name field when editing leave selected the client name respective to the client name of the line that was pressed the button

1 answer

2


The problem is that when picking up the text in the table, it is not matching the value in the combobox because captured table text comes with spaces, and will not match the value of the select.

To solve this, always use .trim(), that will clear the spaces:

$('#alterar_tipo').val($(this).parents('tr').find('td').eq(4).text().trim());

Browser other questions tagged

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