0

For the current code,when the button's clicked, it'll get the value from the done input and display them to the 3 input field respectively.

Let say i'd like to change the value in the done input field, and when the value is updated, the old values from the 3 input fields will be replaced by the new. Now, the updated value just added after the original value instead of replacing it. What's wrong with my code guys?

$(".ok").on('click', function() {
var set = $(this).closest('tr').find('.done').val();
if ( set ){
    $(this).closest('tr').find('.text').each(function(){
        $(this).val( $(this).val() + set );
    })
}
});
<table>
  <tr>
 
    <td>
      <input type="button"  value="ok" class="ok"></td>
    <td>done<input type="text" value="100" class="done" \> </td>
    <td>text1<textarea class="text">hi</textarea> </td>
    <td>text2<textarea class="text">hello</textarea> </td>
    <td>text3<textarea class="text">sup</textarea> </td>

  </tr>
  <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

1
  • remove this $(this).val( $(this).val() + set ); and write this $(this).val( set ); because $(this).val( $(this).val() + set ); is adding values beside input field Commented Jul 5, 2017 at 2:28

3 Answers 3

1

$(".ok").on('click', function() {
  var set = $(this).closest('tr').find('.done').val();
  if (set) {
    $(this).closest('tr').find('.text').each(function() {
      $(this).val($(this).attr("data-value") + set);
    })
  }
});
<table>
  <tr>

    <td>
      <input type="button" value="ok" class="ok"></td>
    <td>done<input type="text" value="100" class="done" \> </td>
    <td>text1<textarea class="text" data-value="hi">hi</textarea> </td>
    <td>text2<textarea class="text" data-value="hello">hello</textarea> </td>
    <td>text3<textarea class="text" data-value="sup">sup</textarea> </td>

  </tr>
  <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

  1. Add a data-* on each textarea.
  2. Use that value instead of the actual value
Sign up to request clarification or add additional context in comments.

1 Comment

glad to help happy coding mate
0

Try this instead:

$(this).val( $(this).val(set));

Comments

0

you don't need to add set to $(this).val(), just update value to set using $(this).val(set);. something like this:

$(".ok").on('click', function() {
var set = $(this).closest('tr').find('.done').val();
if ( set ){
    $(this).closest('tr').find('.text').each(function(){
        $(this).val(set);
    })
}
});
<table>
  <tr>
 
    <td>
      <input type="button"  value="ok" class="ok"></td>
    <td>done<input type="text" value="100" class="done" \> </td>
    <td>text1<textarea class="text">hi</textarea> </td>
    <td>text2<textarea class="text">hello</textarea> </td>
    <td>text3<textarea class="text">sup</textarea> </td>

  </tr>
  <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>

2 Comments

The thing is i have some prefilled text retrieve from the database that i'd like to keep, do u know how to on one hand keep those texts, while updating the value? Many thanks
you can access those values using $(this).val() before updating the values and store them in some variable.

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.