jQuery next()/nextAll()/siblings() + dialog() Gotcha
Posted on June 16, 2012 at 10:31 PM in General, jQuery
So today I was writing up a simple little form hint with a jQuery UI dialog popup with more details on the hint. The code was actually quite simple:
- <!-- the relevant HTML -->
- <div class="fieldHint">
- <a href="##" class="tip-opener">View a full description</a>
- <div class="tip" title="Type Descriptions">{content}</div>
- </div>
- <!-- the relevant jQuery -->
- $( 'a.tip-opener').click(function(e){
- e.preventDefault();
- $(this).next().dialog( 'open' );
- });
Easy enough, right? Wrong. The dialog never opened. Time to debug. I tried each of the following as well, but they all returned 'undefined'.
- $(this).next( 'div.tip' ).dialog( 'open' );
- $(this).nextAll().dialog( 'open' );
- $(this).nextAll( 'div.tip' ).dialog( 'open' );
- $(this).siblings().dialog( 'open' );
- $(this).siblings( 'div.tip' ).dialog( 'open' );
Lots of searching and reading led me nowhere. Frustration was mounting. And then I had a thought:
Hmm. I wonder... maybe jQuery is ignoring the 'tip' div because it is currently hidden??
So I decided to slightly alter the code, resulting in the following:
- <!-- the relevant HTML -->
- <div class="fieldHint">
- <a href="##type-descriptions" class="tip-opener">View a full description</a>
- <div class="tip" id="type-descriptions" title="Type Descriptions">{content}</div>
- </div>
- <!-- the relevant jQuery -->
- $( 'a.tip-opener').click(function(e){
- e.preventDefault();
- $( $(this).attr( 'href' ) ).dialog( 'open' );
- });
Bingo! The dialog now opened as expected.
Hopefully this little post helps someone else, but I really I remember about it the next time I forget the lesson learned today. :-)
Latest Articles
- No recent entries.
Categories
- ColdBox (21) [RSS]
- ColdFusion (92) [RSS]
- Fusebox (3) [RSS]
- General (22) [RSS]
- jQuery (15) [RSS]
- Kalendar (1) [RSS]
- Linux (1) [RSS]
- Mura CMS (1) [RSS]
- Railo (1) [RSS]
- Rants (5) [RSS]
- Transfer (8) [RSS]
- Uni-Form Tag Library (36) [RSS]
Quick Links
Blogs I Read
Calendar
S | M | T | W | T | F | S |
---|---|---|---|---|---|---|
« Sep | ||||||
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Subscribe
Enter a valid email address.
On 6/18/12 at 3:46 PM, James Moberg said:
$(this).next('div').dialog('open');