views:

53

answers:

2

Hi guys, I have been trying to apply the remove function to my html page but with no result until now. I have three rows and they all have a remove button, I would like to create an effect that when you click on the remove button the whole row block disappears. Any help would be deeply appreciated.

HTML

                <div class="row deletedRow">
                    <div class="col1">
                        <table cellspacing="0" cellpadding="0" border="0">
                            <tbody>
                                <tr>
                                    <td><img src="../stylesheets/shore-excursions/images/background/photo1.jpg" alt="" height="124" width="155" /></td>
                                    <td>
                                        <h3>Velit mauris scelerisque risus lorem vatis</h3>
                                        <p>Grand Cayman, Cayman Islands</p>
                                        <ul>
                                            <li>Duration 3.5 hours</li>
                                            <li>Ages 8 & Up</li>
                                            <li>Moderate Activity
                                                <span class="box-tooltip"><img src="../images/shore-excursions/icon-help13x13.gif" width="13" height="13" alt="" />
                                                <div>
                                                    <span class="arrow"></span>
                                                    <p class="red">Lorem ipsum dolor.</p>
                                                    <p>Lorem ipsum dolor sit  nostrud mas consectetur </p>
                                                </div>
                                                </span>                                                     
                                            </li>
                                        </ul>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                    <a href="#"><img src="../stylesheets/shore-excursions/images/buttons/btn_add_to_compare.gif" alt="ADD TO COMPARE" height="31" width="155" /></a>
                                    </td>
                                    <td></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col2">
                        <table cellpadding="0" cellspacing="0" border="0">
                            <tbody>
                                <tr>
                                    <td class="with-border">
                                        <p>Adults:</p><p class="red">$100</p>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <p>Children:</p><p class="red">$60</p>
                                    </td>
                                </tr>
                                 <tr>
                                    <td>
                                        <p class="font11">Limited Available</p><p class="red-font11">Only 6 left!</p>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <a href="#" class="removeRow"><img src="../stylesheets/shore-excursions/images/buttons/btn-remove.png" alt="REMOVE" height="32" width="148" /></a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <a href="#"><img src="../stylesheets/shore-excursions/images/buttons/btn_save_to_favorites.jpg" alt="SAVE TO FAVORITES" height="31" width="153" /></a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>  
                </div>         
                <div class="row deletedRow">
                    <div class="col1">
                        <table cellspacing="0" cellpadding="0" border="0">
                            <tbody>
                                <tr>
                                    <td><img src="../stylesheets/shore-excursions/images/background/photo2.jpg" alt="" height="124" width="155" /></td>
                                    <td>
                                        <h3>Velit mauris scelerisque risus lorem vatis</h3>
                                        <p>Grand Cayman, Cayman Islands</p>
                                        <ul>
                                            <li>Duration 3.5 hours</li>
                                            <li>Ages 7 & Up</li>
                                            <li>Moderate Activity
                                                <span class="box-tooltip"><img src="../images/shore-excursions/icon-help13x13.gif" width="13" height="13" alt="" />
                                                <div>
                                                    <span class="arrow"></span>
                                                    <p class="red">Lorem ipsum dolor.</p>
                                                    <p>Lorem ipsum dolor sit  nostrud mas consectetur </p>
                                                </div>
                                                </span>                                                     
                                            </li>
                                        </ul>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                    <a href="#"><img src="../stylesheets/shore-excursions/images/buttons/btn_add_to_compare.gif" alt="ADD TO COMPARE" height="31" width="155" /></a>
                                    </td>
                                    <td></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col2">
                        <table cellpadding="0" cellspacing="0" border="0">
                            <tbody>
                                <tr>
                                    <td class="with-border">
                                        <p>Adults:</p><p class="red">$100</p>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <p>Children:</p><p class="red">$60</p>
                                    </td>
                                </tr>
                                 <tr>
                                    <td>
                                        <p class="font11">Limited Available</p><p class="red-font11">Only 6 left!</p>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <a href="#" class="removeRow"><img src="../stylesheets/shore-excursions/images/buttons/btn-remove.png" alt="REMOVE" height="32" width="148" /></a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <a href="#"><img src="../stylesheets/shore-excursions/images/buttons/btn_save_to_favorites.jpg" alt="SAVE TO FAVORITES" height="31" width="153" /></a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>  
                </div>         
                <div class="row">
                    <div class="col1">
                        <table cellspacing="0" cellpadding="0" border="0">
                            <tbody>
                                <tr>
                                    <td><img src="../stylesheets/shore-excursions/images/background/photo3.jpg" alt="" height="124" width="155" /></td>
                                    <td>
                                        <h3>Velit mauris scelerisque risus lorem vatis</h3>
                                        <p>Grand Cayman, Cayman Islands</p>
                                        <ul>
                                            <li>Duration 3.5 hours</li>
                                            <li>Ages 7 & Up</li>
                                            <li>Moderate Activity
                                                <span class="box-tooltip"><img src="../images/shore-excursions/icon-help13x13.gif" width="13" height="13" alt="" />
                                                <div>
                                                    <span class="arrow"></span>
                                                    <p class="red">Lorem ipsum dolor.</p>
                                                    <p>Lorem ipsum dolor sit  nostrud mas consectetur </p>
                                                </div>
                                                </span>                                                     
                                            </li>
                                        </ul>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                    <a href="#"><img src="../stylesheets/shore-excursions/images/buttons/btn_add_to_compare.gif" alt="ADD TO COMPARE" height="31" width="155" /></a>
                                    </td>
                                    <td></td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="col2">
                        <table cellpadding="0" cellspacing="0" border="0">
                            <tbody>
                                <tr>
                                    <td class="with-border">
                                        <p>Adults:</p><p class="red">$100</p>
                                    </td>
                                </tr>

                                <tr>
                                    <td>
                                        <p>Children:</p><p class="red">$60</p>
                                    </td>
                                </tr>
                                 <tr>
                                    <td>
                                        <p class="font11">Limited Available</p><p class="red-font11">Only 6 left!</p>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <a href="#" class="removeRow"><img src="../stylesheets/shore-excursions/images/buttons/btn-remove.png" alt="REMOVE" height="32" width="148" /></a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <a href="#"><img src="../stylesheets/shore-excursions/images/buttons/btn_save_to_favorites.jpg" alt="SAVE TO FAVORITES" height="31" width="153" /></a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>  
                </div>         

Thank you in advance!

+3  A: 

You can use .closest() to climb up the parents to your class="row" <div>, like this:

$(".removeRow").click(function() {
  $(this).closest("div.row").remove();
});
Nick Craver
Just a question, what's the differens between prevAll() and closest() (I would use prevAll).
Tim
@Tim `prevAll()` finds all previous siblings. Means all the previous entities who have the same parent as the element in which currently you are.
Alpesh
@Tim - @Alpesh is correct, `.prevAll()` doesn't work here, it's looking at a different set of elements, not including the ancestors.
Nick Craver
That make's things clear! Thanks a lot!
Tim
This snippet of code helped me alot, I guess my last question would be if is possible to add some kind of effect like fadeOut or slideUp?
Ozzy
@Ozzy - Yeah you can do that, like this: `$(this).closest("div.row").fadeOut(function() { $(this).remove(); });` a `slideUp()` would be the same way, but may look funny as the content re-flows, try it out and see which effect you like.
Nick Craver
+1  A: 

Here is a JSFiddle I made that basically does a loop that will walk up the DOM tree until it finds the parent node that has the class "row" or the body. If the node with the class is found it will remove it otherwise nothing is removed.

JSFiddle Example

subhaze
I'm curious, why would you do this when [`.closest()`](http://api.jquery.com/closest/) is already built-in? Unless you're on jQuery < 1.3 this seems a bit redundant.
Nick Craver
This script works fine, the only problem here is if you click on the first 'REMOVE' link it will remove the rest of the rows before removing itself.
Ozzy
Very true, I had this opened up in a tab to answer later and did not see the above answer. I don't use JQuery so I took a quick look at the API and just tried to lend a helping hand.
subhaze
@Ozzy I don't see that happening... I tested on FF and Chrome, here is an updated link that has the row number at top. All seems to be deleted correctly.http://www.jsfiddle.net/subhaze/LdAmy/3/
subhaze