Insert value in Input Value with JS

Asked

Viewed 1,931 times

0

I’m trying to bring a value that when clicking a button brings in an input that works but the value does not appear inside the Value input so I can’t capture this value via PHP.

Follows the code

https://jsfiddle.net/felipefranco/2fkje6m4/3/

Knob

<a href="#my_modal" data-toggle="modal" data-fornecedor-id="<?php echo $rowPedido->id; ?>"><img src="img/lupa.png"  ></a>

Input

<input type="text" name="idPedido" id="idPedido" value="" />

JS

  $('#my_modal').on('show.bs.modal', function(e) {
var idPedido = $(e.relatedTarget).data('fornecedor-id');

document.getElementById('idPedido').value = idPedido;    

});

2 answers

1


To display the value in the value attribute as you wish, do it as follows:

$(document).on("click", ".open-AddBookDialog", function () {
     var myBookId = $(this).data('id');
     $(".modal-body #bookId").attr('value', myBookId );
});

Note that it is used .attr('value', myBookId ) instead of .val(myBookId )

https://jsfiddle.net/2fkje6m4/7/

  • Thanks Gustavo worked out that was right!!

1

Try it this way and tell me if it worked out what you wanted:

$('#my_modal').on('show.bs.modal', function(event) {

  var $target = [];
      $target['fornecedor-id'] = $(event.relatedTarget).data('fornecedor-id');
      $("#idPedido").val($target['fornecedor-id']);

});
  • This code does the same thing as what I’m using it prints on the screen but does not appear within Value=" "

  • I created a Template in Jsfiddle if you look it prints the result on the screen but when you look with firebug the value is blank. https://jsfiddle.net/felipefranco/2fkje6m4/3/

  • to appear within the value, this has to be printed on the page before the event: <?php echo $rowPedido->id; ?>

  • but this is obvious... you are doing jquery... you have to inspect the element after javascript.

  • There’s nothing wrong with the code?

  • When you inspect an html element, javascript changes will never appear. Because javascript does not send this to the server. To see changes occur, you have to watch the source code while sending the request.

  • But when I give $_POST in PHP that value comes blank

  • But then there is another problem, you have to make a form. an input alone does not even send. Unless it is an ajax request. and the value of the input is captured, for a sending object.

  • I know that here only brought a part of the code this input is inside a Form what is not working is to capture this value because the field Value is blank

  • Look at that one another example. Imagine this data is being sent to a PHP page.

  • Instead of /echo/json/ it would be in PHP: $_POST['dado'];

Show 7 more comments

Browser other questions tagged

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