Solved

JQuery ASP Gridview Closest Cell Value?

Posted on 2014-02-04
6
761 Views
Last Modified: 2014-02-04
I have a click event to get the clicked cell value and the cell in the row below. Trying to sum two cells. The code below worked fine before I added the 'closest' syntax. Ultimately I need to loop through the entire grid comparing grid cells. Help

<script src="http://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<script type="text/javascript">

    $(document).ready(function ()
    {
        $("#GridView1 td").click(function ()
         {
            alert('top:' = $(this).html() + '  bottom:' + $(this).closest('tr').find('td').html() );

            //alert($(this).html());
        });
    });

</script>
0
Comment
Question by:WorknHardr
  • 3
  • 3
6 Comments
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39833960
$(this).closest('tr') will find the current row of the clicked TD. You then run find('td') which will find all TDs in that row as an array, so you can't just read the html() of it. You'd need to loop through the array.

Which cell are you expecting to get by using the closest() function
0
 

Author Comment

by:WorknHardr
ID: 39834015
I need to compare value of 'cell1' with value of 'cell2'

[Gridview]
Header       Column1
Row1            [cell1]
Row2            [cell2]
...
Footer

Open in new window

0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 400 total points
ID: 39834052
OK. Basically, you'll need to get the index of the clicked TD, and then traverse to the next TR, and then select the TD from the same index:

$('#GridView1 td').click(function() {
    TDindex = $(this).index();
    topVal = $(this).html();
    bottomVal = $(this).closest('tr').next().children().eq(TDindex).html();

    alert('Top:' + topVal + ' | Bottom:' + bottomVal);    
})

Open in new window

You can see a working copy here: http://jsfiddle.net/ChrisStanyon/KXx7c/
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:WorknHardr
ID: 39834089
Excellent!

Curious, how do I use the bottomVal object?

It does NOT work like this for sure:

 if (topVal != bottomVal) {
                $(bottomVal).addClass("grdCell");
            }

<style type="text/css">
.grdCell
{
    background-color:Yellow;
}
</style>
0
 
LVL 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 400 total points
ID: 39834140
OK. In my code bottomVal is just the html() value of the TD. If you want to get the actual TD, then just drop off the html() call at the end of the selector:

//get the html value
topVal = $(this).html();

//get the actual TD from below
bottomVal = $(this).closest('tr').next().children().eq(TDindex);

//Now compare the 2
if (topVal != bottomVal.html()) {
    bottomVal.addClass('grdCell');
}

Open in new window

I've updated the fiddle for you to see it in action
0
 

Author Closing Comment

by:WorknHardr
ID: 39834159
Excellent! thx...
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question