1

I did an example about replacing the input value when the row is deleted but is not working (this is not a static example).

<script src="./edit_files/prototype.js" type="text/javascript"></script>
<script src="./edit_files/application.js" type="text/javascript"></script>

<div class="contact">
<table border="0"> 
  <tr>
    <td><select class="position_id" id="obj_client_contact_attributes__position_id" name="obj_client[contact_attributes][][position_id]"><option value="1" selected="selected">INTELIGENT</option><option value="2">OTHER</option></select></td>

      <td><input class="should_change_value" id="obj_client_contact_attributes__phone_mail" name="obj_client[contact_attributes][][phone_mail]"  type="text" value="[email protected]"/></td>

    <td>        
      <a href="#" onclick="mark_for_destroy_contact(this,true); return false;">DELETE</a>
        <input id="obj_client_contact_attributes__id" name="obj_client[contact_attributes][][id]" type="hidden" value="16594"/>
        <input class="should_destroy" id="obj_client_contact_attributes__should_destroy" name="obj_client[contact_attributes][][should_destroy]" type="hidden"/>
    </td>
  </tr>
</table>
</div>

<div class="contact">
<table border="0"> 
  <tr>
    <td><select class="position_id" id="obj_client_contact_attributes__position_id" name="obj_client[contact_attributes][][position_id]"><option value="1" selected="selected">INTELIGENT</option><option value="2">OTHER</option></select></td>

      <td><input class="should_change_value" id="obj_client_contact_attributes__phone_mail" name="obj_client[contact_attributes][][phone_mail]" type="text" value="ONLY THE INPUT WILL BE [email protected] IF I CLICK ON DELETE"/></td>

    <td>        
      <a href="#" onclick="mark_for_destroy_contact(this,true); return false;">DELETE</a>
        <input id="obj_client_contact_attributes__id" name="obj_client[contact_attributes][][id]" type="hidden" value="16594"/>
        <input class="should_destroy" id="obj_client_contact_attributes__should_destroy" name="obj_client[contact_attributes][][should_destroy]" type="hidden"/>
    </td>
  </tr>
</table>
</div>

Here is the application.js file:

function mark_for_destroy_contact(element,should_destroy,should_change_value) {
 var element_text = $(element).up('.contact').down('.position_id',0);
 element_text.className = 'position_id';
 element_text.value = '';

 if (should_destroy) {
   $(element).next('.should_destroy').value = 1;
 }

 $(element).up('.contact').hide();
}  

I tried this code but only works if I remove the first row.

function mark_for_destroy_contact(element,should_destroy,should_change_value) {
  var element_text = $(element).up('.contact').down('.position_id',0);
  element_text.className = 'position_id';
  element_text.value = '';

  $('should_change_value').update("[email protected]");

  if (should_destroy) {
   $(element).next('.should_destroy').value = 1;
  }
  $(element).up('.contact').hide();
}

Here is the live example in jsfiddle

Here is the example download on Github but is not replacing the input value correctly

3
  • Read very carefully in the jQuery doc what .next('.should_destroy') actually does. It looks at the next sibling element and returns something ONLY if that very next element matches the selector '.should_destroy'. That is often not what people want or what people expect when they use it. I can't tell from your code if this is your issue or not, but it is a common one. Commented Apr 24, 2015 at 0:36
  • Do you want to delete the entire row when you click on the delete link? Commented Apr 27, 2015 at 15:31
  • Yes, i did it that here github.com/sayayingod/… but I don't only want to delete the row also I want to change the input value to [email protected] on each row deleted. Commented Apr 27, 2015 at 15:33

1 Answer 1

1

Ok I got it, you want to change the input value when the row is deleted, so do this:

function mark_for_destroy_contact(element,should_destroy,should_change_value) {
   var element_text = $(element).up('.contact').down('.position_id',0);
   element_text.className = 'position_id';
   element_text.value = '';

   var element_text2 = $(element).up('.contact').down('.should_change_value',0);
   element_text2.className = 'should_change_value';
   element_text2.value = '[email protected]';   

   if (should_destroy) { $(element).next('.should_destroy').value = 1;}

   $(element).up('.contact').hide();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much..didn't know how to deal with this.

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.