0

I have a form that if user clicks remove, i need to remove the whole row.

            <div id='a_<?php echo $a;?>'>
                    <div class='leftDesc'>Code</div>
                    <div class='inputText'><input type='text' name='code[]' value='<?php echo $code;?>' id='code[]'></div>
                    <div class='centerDesc'>Description</div>
                    <div class='inputText'><input type='text' name='desc[]' value='<?php echo $desc;?>' size='32' maxlength='32' id='desc[]'></div>
                    <div class='centerDesc'><input type='button' class='remDisp' value='Remove Code' id='removeCode_<?php echo $a;?>'></div>
            </div>

when user clicks remove button, i want to remove the whole row (the div a_$a)

how do i write basically:

$(this).parent().parent().remove();
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
bart2puck
  • 2,432
  • 3
  • 27
  • 53

1 Answers1

0

If you're looking for an alternative approach that isn't dependent on the element position, associate them by a data attribute or class, like this: Live demo here (click).

<div id="bar"></div>
<button class="remDisp" data-foo="bar">Some Button</button>

<script>
  $(document).ready(function() {

    var buttons = $('.remDisp'); //get a reference to your buttons
    buttons.click(function() { //add the click function to the buttons
      var foo = $(this).attr('data-foo'); //get "foo" attr (value of "bar" in this case)
      $('#'+foo).remove(); //find the element with id of foo's value (bar)
    });

  });
<script>

Do you mean "Where do I put my javascript code and how do I attach it to the button?"

<script>
  $(document).ready(function() {

    var buttons = $('.remDisp'); //get a reference to your buttons
    buttons.click(function() { //add the click function to the buttons
      $(this).parent().parent().remove(); //remove the grandparent of the clicked button.
    });

  });
<script>
m59
  • 43,214
  • 14
  • 119
  • 136