Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

JQuery ASP Gridview Closest Cell Value?

Posted on 2014-02-04
6
Medium Priority
?
817 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 44

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 44

Accepted Solution

by:
Chris Stanyon earned 1600 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 44

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 1600 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
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…
Suggested Courses

636 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