Solved

JQuery ASP Gridview Closest Cell Value?

Posted on 2014-02-04
6
768 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 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
converting document.write() to innerHTML 7 80
How do I post more than 1 item to php backend 24 60
asp.net mvc5 6 33
Convert complicated date to yyyy-mm-dd format 22 50
Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

734 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