Kamrul
Kamrul

Reputation: 23

Textarea editable except values from inputs

I have two input fields and one textarea. When I type something in the input fields their .val() are automatically added to the textarea.

It is working fine, but I want these values in the textarea as read-only.

I want the textarea to behave like this:
I could type extra text but can't delete the text which is obtained from two input fields.

$(function() {
  var text1 = $('#text1');
  var text2 = $('#text2');
  var description = $('#description');

  function onChange() {
    description.val("Chesis No:  " + text1.val() + ", Engine No:  " + text2.val());

    var base = description.val();
    var regex = new RegExp("^" + base, "i");
    $('#description').on("input", function(ev) {
      var query = $(this).val();
      if (!regex.test(query)) {
        //ev.preventDefault();
        $(this).val(base);
      }
    });
  }

  $('#text1')
    .change(onChange)
    .keyup(onChange);

  $('#text2')
    .change(onChange)
    .keyup(onChange);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text1" placeholder="Chassis No" class="form-control" />
<input type="text" id="text2" placeholder="Engine No " class="form-control" />
<textarea rows="4" cols="50" name="postContent" id="description"></textarea>

https://jsfiddle.net/dreamfighterr/x4v0sxt8/

Upvotes: 2

Views: 232

Answers (3)

Takit Isy
Takit Isy

Reputation: 10081

Updated answer
Using only one textarea

I played a little more with your code and ended-up with this really easy solution:

  • Split all the lines of the textarea,
  • Put the values of the inputs in the first line (That simulates the "read-only" for this line)
  • Join back all the lines.

Here is a working snippet:
(See comments in my code)

$(function() {
  var text1 = $('#text1');
  var text2 = $('#text2');
  var description = $('#description');

  // TAKIT: Changed all in this function
  function onChange() {
    var desc = description.val().split('\n');
    
    // TAKIT: Modified here
    var texts = []; // TAKIT: Added an array to make it easier
    if (text1.val())  texts.push("Chesis No: " + text1.val());
    if (text2.val())  texts.push("Engine No: " + text2.val());
    desc[0] = texts.join(', ');
    
    if (desc.length == 1) desc[1] = '';
    description.val(desc.join('\n'));
  }

  // TAKIT: Suggestion to simplify
  $('.form-control').on('keyup change', onChange);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text1" placeholder="Chassis No" class="form-control" />
<input type="text" id="text2" placeholder="Engine No" class="form-control" />
<br><br>
<textarea rows="6" cols="50" name="postContent" id="description" class="form-control"></textarea>

I think it can't be easier!

Hope it helps.

⋅ ⋅ ⋅


Old answer
Adding a new textarea

As you can't have a "partially-read-only" element, I suggest you that approch instead:

  • Add a new textarea element which id="text3",
  • Simplify your code using $('[id^="text"]') to select all elements which id starts with “text”,
  • Add readonly attribute on the #description textarea that merges the text from the input elements.

That way, the resulting content in #description will be accessible to select/copy/paste, but would not be modifiable.

Working snippet:

$(function() {
  var text1 = $('#text1');
  var text2 = $('#text2');
  var text3 = $('#text3'); // TAKIT: Added
  var description = $('#description');

  function onChange() {
    description.val("Chesis No:  " + text1.val() + ", Engine No:  " + text2.val() + '\n' + text3.val()); // TAKIT: Added

    var base = description.val();
    var regex = new RegExp("^" + base, "i");
    $('#description').on("input", function(ev) {
      var query = $(this).val();
      if (!regex.test(query)) {
        //ev.preventDefault();
        $(this).val(base);
      }
    });
  }

  // TAKIT: Suggestion to simplify
  $('[id^="text"]')
    .change(onChange)
    .keyup(onChange);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text1" placeholder="Chassis No" class="form-control" />
<br>
<input type="text" id="text2" placeholder="Engine No " class="form-control" />
<br>
<textarea id="text3" placeholder="Comments" class="form-control"></textarea>
<br><br>
<textarea rows="4" cols="50" name="postContent" id="description" readonly></textarea>

Hope it helps!

Upvotes: 1

mostafa tourad
mostafa tourad

Reputation: 4388

You need to keep track of the two inputs value and instead of directly insert them to the text area , you will create two strings and every time the user change any input append the input file the one of the strings then append that string to your text area , see the example below , as you can see I use the blur event instead of change to get the whole value of changed input

see the complete example to understand the solution

$(function() {
  var firstString ='';
  var secondString = '';
  var text1 = $('#text1');
  var text2 = $('#text2');
  var description = $('#description');

  function onChange() {
    description.val("Chesis No:  " + text1.val() + ", Engine No:  " + text2.val());

    var base = description.val();
    var regex = new RegExp("^" + base, "i");
    $('#description').on("input", function(ev) {
      var query = $(this).val();
      if (!regex.test(query)) {
        //ev.preventDefault();
        $(this).val(base);
      }
    });
  }
function ontext1Blur(){
  firstString+=text1.val()
 description.val("Chesis No:  " + firstString + ", Engine No:  " + text2.val());
 var base = description.val();
    var regex = new RegExp("^" + base, "i");
    $('#description').on("input", function(ev) {
      var query = $(this).val();
      if (!regex.test(query)) {
        //ev.preventDefault();
        $(this).val(base);
      }
    });
}
function ontext2Blur(){
  secondString+=text2.val()
 description.val("Chesis No:  " + firstString + ", Engine No:  " + secondString);
 var base = description.val();
    var regex = new RegExp("^" + base, "i");
    $('#description').on("input", function(ev) {
      var query = $(this).val();
      if (!regex.test(query)) {
        //ev.preventDefault();
        $(this).val(base);
      }
    });
}
  $('#text1')
    .blur(ontext1Blur)

  $('#text2')
    .blur(ontext2Blur);

});
textarea {

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text1" placeholder="Chassis No" class="form-control" />
<input type="text" id="text2" placeholder="Engine No " class="form-control" />
<textarea  rows="4" cols="50" name="postContent" id="description"></textarea>

Upvotes: 0

Hardik Prajapati
Hardik Prajapati

Reputation: 177

$(function() {
  var text1 = $('#text1');
  var text2 = $('#text2');
  var description = $('#description');

  function onChange() {
    description.val("Chesis No:  " + text1.val() + ", Engine No:  " + text2.val());

    var base = description.val();
    var regex = new RegExp("^" + base, "i");
    $('#description').on("input", function(ev) {
      var query = $(this).val();
      
    });
  }

  $('#text1')
    .change(onChange)
    .keyup(onChange);

  $('#text2')
    .change(onChange)
    .keyup(onChange);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text1" placeholder="Chassis No" class="form-control" />
<input type="text" id="text2" placeholder="Engine No " class="form-control" />
<textarea rows="4" cols="50" name="postContent" id="description"></textarea>

Upvotes: 0

Related Questions