1

I have a code like these:

var fails = 'fail';
var sucs = 'success';

function showResult(state){
	if(state){
      document.getElementById("hasil").value=sucs;
    }else{
      document.getElementById("hasil").value=fails;
    }									
}
<form>
  <table border='1'>
    <tr>
      <th>TARGET</th>
      <th>RESULT</th>
      <th>NOTES</th>
    </tr>
    <tr>
      <td><input type='checkbox' name='target' onclick='showResult(this.checked);' /></td>
      <td><textarea name='result[]' id='hasil'>fail</textarea></td>       <td><textarea name='notes[]' class='form-control'></textarea></td>
    </tr>
    <tr>
      <td><input type='checkbox' name='target' onclick='showResult(this.checked);' /></td>
      <td><textarea name='result[]' id='hasil'>fail</textarea></td>       <td><textarea name='notes[]' class='form-control'></textarea></td>
    </tr>
    <tr>
      <td><input type='checkbox' name='target' onclick='showResult(this.checked);' /></td>
      <td><textarea name='result[]' id='hasil'>fail</textarea></td>       <td><textarea name='notes[]' class='form-control'></textarea></td>
    </tr>
</form>

I want to create that checkbox, if it's clicked, it will change textarea on that row to success. If I uncheck, it will change back to fail. Can anyone help me?

4
  • getElementsByClassName is not name, getElementsByClassName returns a collection.... Commented Oct 11, 2016 at 4:03
  • I try to change it with ID but it only works for the first row... Commented Oct 11, 2016 at 4:06
  • 1
    Because ids are singular. Hopefully someone can show you how to get the reference to the checkbox. Select the parent tr, find the text area and set the value. I can not code that on my phone. Commented Oct 11, 2016 at 5:09
  • I will try find way to do that... thanks before.. Commented Oct 11, 2016 at 6:13

1 Answer 1

1

it only works for the first row.

As @epascarello said, because ids are singular. so you need to select the parent tr, find the text area and set the value.

You can do in this way.

$('[type=checkbox]').change(function() {

  if ($(this).is(":checked")) {
    var $row = $(this).parents('tr');
    $row.find('textarea[name="result[]"]').text("sucess");
  } else {
    var $row = $(this).parents('tr');
    $row.find('textarea[name="result[]"]').text("fail");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
  <table border='1'>
    <tr>
      <th>TARGET</th>
      <th>RESULT</th>
      <th>NOTES</th>
    </tr>
    <tr>
      <td>
        <input type='checkbox' name='target' />
      </td>
      <td>
        <textarea name='result[]' id='hasil'>fail</textarea>
      </td>
      <td>
        <textarea name='notes[]' class='form-control'></textarea>
      </td>
    </tr>
    <tr>
      <td>
        <input type='checkbox' name='target' />
      </td>
      <td>
        <textarea name='result[]' id='hasil'>fail</textarea>
      </td>
      <td>
        <textarea name='notes[]' class='form-control'></textarea>
      </td>
    </tr>
    <tr>
      <td>
        <input type='checkbox' name='target' />
      </td>
      <td>
        <textarea name='result[]' id='hasil'>fail</textarea>
      </td>
      <td>
        <textarea name='notes[]' class='form-control'></textarea>
      </td>
    </tr>
</form>

Sign up to request clarification or add additional context in comments.

3 Comments

hello, it works smooth in snippet, but not in my code. Is it need to be placed on head or body? or it's not matter?
Could you tell me on which technology you are working with? and as of now considered HTML, so you can put this in header tag. in $(document).ready(function(){yourcode});
I'm very sorry... I missed bracket after function.... It works perfectly now.. thank you so much!

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.