Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 20th, 2014, 09:05 PM
Registered User
 
Join Date: Dec 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Little trouble with JQuery .prev method

On page 385, you show an example of how to use prev and parent methods to traverse the DOM. In the middle of page, you show a snippet:

alert($('#DemoTable td:contains("3")').prev()[0].innerHTML);

How exactly is this used? I cannot get his to work...a test page I created loads but nothing happens. I do in fact have a table with the ID of DemoTable and I do have a cell with "3" (1-6 sequential cells).


Code:
            <table align="center" border="1" width="500" id="DemoTable">
        <tr>
            <td>1</td>
            <td>2</td>
        </tr>
        <tr>
            <td>3</td>
            <td>4</td>
        </tr>
        <tr>
            <td>5</td>
            <td>6</td>
        </tr>
    </table>

        <script type="text/javascript">


            alert($('#DemoTable td:contains("3")').prev()[0].innerHTML);


        </script>


Thanks!
 
Old October 21st, 2014, 04:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

prev() gets the previous sibling of the selected item. This means that it tries to get the previous sibling of the "3" cell. However, since it's the first child item of the parent row, nothing is matched.

Either use:

alert($('#DemoTable td:contains("4")').prev()[0].innerHTML);

to get the cell in the right with "contains" and then the cell on the left using prev()

or use next():

alert($('#DemoTable td:contains("3")').next()[0].innerHTML);

to get the cell on the left using using contains and then next() to get the one on the right.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old October 21st, 2014, 08:17 PM
Registered User
 
Join Date: Dec 2013
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

OK, thanks for the feedback.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble running method from WPF UserControl Button's Click dgomez BOOK: Professional C# 2012 and .NET 4.5 0 September 23rd, 2013 04:58 PM
Trouble with jQuery try it out p.370 [email protected] BOOK: Beginning ASP.NET 4 : in C# and VB 3 December 4th, 2011 06:50 AM
Trouble registering jquery accordion on code behind ysfkay ASP.NET 3.5 Professionals 0 March 11th, 2010 08:14 AM
OnCheckedChanged trouble with codebehind method theycallmetish General .NET 1 July 9th, 2005 01:33 PM
Trouble with date method... budman Javascript 2 May 25th, 2004 10:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.