Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-08-2010, 05:20 PM   PM User | #1
lauthiamkok
Regular Coder

 
Join Date: Dec 2008
Posts: 117
Thanks: 14
Thanked 0 Times in 0 Posts
lauthiamkok is an unknown quantity at this point
jquery: next() and nested elements

Hi,
I have nested elements like below,

Code:
<div>
		<p>Hello Again</p>
		<p class="selected"></p>
	</div>
	
	<div>
		<p>Hello Again</p>
		<p class="selected"></p>
	</div>
	
	<div>
		<p>Hello Again</p>
		<p class="selected"></p>
	</div>
I want to add text in the 'selected' paragraph in the second div,

Code:
<script>$("div:first").next('.selected').text("selected");</script>
it won't work at all. please let me know if you have any ideas.

Many thanks,
Lau
lauthiamkok is offline   Reply With Quote
Old 02-08-2010, 08:22 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Code:
<script>$("div:first p.selected").html("selected");</script>
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote
Old 02-08-2010, 08:55 PM   PM User | #3
Fumigator
UE Antagonizer


 
Fumigator's Avatar
 
Join Date: Dec 2005
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
Fumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of lightFumigator is a glorious beacon of light
Quote:
Originally Posted by rnd me View Post
Code:
<script>$("div:first p.selected").html("selected");</script>
That doesn't select the second div, it selects the first.

If you want the second div, you can use $("div:eq(1)"), and to change the text of the paragraph where class="selected" use $("div:eq(1) p.selected").text("new text here").

The reason why your code wasn't working is because .next() is like a for loop on a group of selected elements, but your group of selected elements had only one element in it-- the first div.

(EDIT) No, I was mistaken, that's not why. I was misreading the manual. The reason why is because you giving the .next() function a selector of ".selected", which is an additional filter applied to the next element. So in your case, the second div would only be selected if it has a class="selected". It doesn't check the div's children for this selector.
__________________

Last edited by Fumigator; 02-08-2010 at 09:04 PM..
Fumigator is offline   Reply With Quote
Old 02-08-2010, 09:30 PM   PM User | #4
lauthiamkok
Regular Coder

 
Join Date: Dec 2008
Posts: 117
Thanks: 14
Thanked 0 Times in 0 Posts
lauthiamkok is an unknown quantity at this point
Quote:
Originally Posted by Fumigator View Post
That doesn't select the second div, it selects the first.

If you want the second div, you can use $("div:eq(1)"), and to change the text of the paragraph where class="selected" use $("div:eq(1) p.selected").text("new text here").

The reason why your code wasn't working is because .next() is like a for loop on a group of selected elements, but your group of selected elements had only one element in it-- the first div.

(EDIT) No, I was mistaken, that's not why. I was misreading the manual. The reason why is because you giving the .next() function a selector of ".selected", which is an additional filter applied to the next element. So in your case, the second div would only be selected if it has a class="selected". It doesn't check the div's children for this selector.
ello! thanks for guide!

eq() - now I know that this is what I should have looked into!

thanks
lauthiamkok is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:32 AM.


Advertisement
Log in to turn off these ads.