0

I want to use Jquery on.(input, [...]) function to display what a user types in another input field.

Here's my code:

$(document).ready(function() {
  $("input.inputtext").on('input', function() {
    var inputtext = $(".inputtext").val();
    $(".showinput").val(inputtext);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="inputs">
  <input type="text" class="inputtext">
  <div>
    <div class="inputs">
      <input type="text" class="inputtext">
      <div>
        <div class="inputs">
          <input type="text" class="inputtext">
          <div>
            <input type="text" class="showinput">

However, the code only shows the entered text for the first input field.

I think I have to use a for loop but I dont't know how to.

2 Answers 2

2

This will work (value from actual field using 'this'):

$(document).ready(function(){
  $("input.inputtext").on('input', function (){
  var inputtext= $(this).val();    
  $(".showinput").val(inputtext);
 });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but I want only the inputed value to be in the showinput field.
@Efoskhi I see what you mean now:) you want to see the input from an actual field. see my edited answer
0

Thanks y'all. I was missing a "this" in the var inputtext syntax.

The correct script is :

$(document).ready(function(){
    $("input.inputtext").on('input', function (){
    var inputtext=$(this, ".inputtext").val();
    $(".showinput").val(inputtext);
  });
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.