To Assign a js variable to an HTML value element using jquery

Asked

Viewed 21 times

-1

I got this code that’s picking up the clicker event

$(function(){
  $('.js-editEstoque').on("click", function(){
    var descricao = $(this).attr('data-name-Estoque');
    var id = $(this).attr('data-id-Estoque');
    $('.js-atribuicao-name').append(descricao);
  });  
});

this is my html

<input type="text" name="editNameDescription" id='editNameDescription' value="" class="form-control input uppercase js-atribuicao-name" maxlength="40" required>
<input type="hidden" name="idEstoque" value="" class="js-atribuicao-id">

I want to put the two variables above inside each element of HTML value, it’s something simple but I’m having difficulties :/

  • Welcome to [en.so]. I’ve come to realize that you’re creating multiple questions for the same problem, when that won’t actually help you get an answer any faster. Instead, read the guide [Ask] and do a [tour] to learn a little more about the operation of the site and thus increase your chances of getting a good answer.

  • 1

1 answer

-1


You have to use the method val(). When emptied ex: .val() it returns the value, when filled it changes the value of the element to what to put inside the parentheses .val('ola')

$(function(){
  $('.js-editEstoque').on("click", function(){
    var descricao = $(this).attr('data-name-Estoque');
    var id = $(this).attr('data-id-Estoque');

    $('.js-atribuicao-id').val(id);
    $('.js-atribuicao-name').val(descricao);
  });  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
1<input type="text" name="editNameDescription" id='editNameDescription' value="" class="form-control input uppercase js-atribuicao-name" maxlength="40" required>


2<input type="text" name="idEstoque" value="" class="js-atribuicao-id">

  • why the down vote?

Browser other questions tagged

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