receive what is written from an input

Asked

Viewed 51 times

2

I have the following situation where I have this input

<input type='text' class='form-control' value='' id='textInput'>

and I have the span

<span date-dados='' class='j_details_action '></span>

the goal is to take what is typed in the input and put inside the span attribute date-data the code works more the input is inside a foreach where it can have more than one input when it has two input the java script works only for the first input . each input has a span example

<input type='text' class='form-control' value='' id='textInput'>
<span date-dados='' class='j_details_action '></span>

inside a foreach would look like this

<input type='text' class='form-control' value='' id='textInput'>
<span date-dados='' class='j_details_action '></span>

<input type='text' class='form-control' value='' id='textInput'>
<span date-dados='' class='j_details_action '></span>

already has the following code in jquery

$('#textInput').on('keyup', function(){
  $(".j_details_action").attr("date-dados", $('#textInput').val()); 

});

  • You can do this only with JS/jQuery. PHP runs on the client side and it won’t help you with this problem. Have you tried anything? Could [Edit] and add the code and describe what was the result obtained?

1 answer

4

You can do this using javascript. With Jquery it is very easy to do, here is an example:

<html>
  <head>
    <meta charset="utf-8">
    <title>One-way Data Binding com jQuery</title>
    <script src="//code.jquery.com/jquery-2.0.3.min.js"></script>
  </head>
  <body>
    Name:<input id="textInput" type="text"/>
    <br />
    <br />
    Olá <span id="nameDiv"></span>!
    <script>
      $('#textInput').on('keyup', function(){
        $('#nameDiv').html($('#textInput').val());
      });
    </script>
  </body>
</html>
  • This "date-data" is an attribute of its span tag that has the class "j_details_action". It would be interesting to put an id on this element. The way it is, to change it would look like this: $(". j_details_action"). attr("date-data", $('#textInput'). val());

Browser other questions tagged

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