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 11-13-2012, 08:24 PM   PM User | #1
CHEWX
Regular Coder

 
Join Date: Dec 2010
Posts: 124
Thanks: 17
Thanked 6 Times in 6 Posts
CHEWX is an unknown quantity at this point
Jquery nth term append

Hey,

Not sure if this is the right section, but I am currently running a wordpress loop for the search page. It displays the results in two different templates.

One of the templates I want to apply a clear both to the second one.

Link: http://admiretheweb.com/?s=the

The loop works like so.

Get posts, if in cat xx show in template a else show the rest in template b.

Templates b needs a clear on the second div, I did this by the following:

Code:
$(document).ready(function() {
$("#article-inspir:nth-child(2n)").append("<div style='clear:both;'></div>");
});
I've tried wrapping the div different quote styles, but it's not selecting the nth-child and applying the clear.

E.g:

Code:
$(document).ready(function() {
$("#article-inspir:nth-child(2n)").append("<div style="clear:both;"></div>");
});
Code:
$(document).ready(function() {
$("#article-inspir:nth-child(2n)").append('<div style="clear:both;"></div>');
});
Why ?

Last edited by CHEWX; 11-14-2012 at 08:48 PM..
CHEWX is offline   Reply With Quote
Old 11-13-2012, 08:55 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
I think you fell for a common misunderstanding about the nth-child selector

#yourid:nth-child(2n) does NOT mean "select the nth child of the element with id=yourid"

It means: Select all elements with id=yourid (there can be only one!) and then filter those elements so that only those elements remain that are themselves the correct nth-child of their parents.

On top of that: Every id attribute on the same page can appear only once! You chose to use the same id="article-inspir" for several <article> elements which is illegal. So you should go for class="article-inspir" instead and change the selector to ".article-inspir"
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
CHEWX (11-14-2012)
Old 11-13-2012, 09:02 PM   PM User | #3
CHEWX
Regular Coder

 
Join Date: Dec 2010
Posts: 124
Thanks: 17
Thanked 6 Times in 6 Posts
CHEWX is an unknown quantity at this point
Ok, regardless of the illegal ID use. This wouldn't work then ?

As I would be using #main :nth-child(2n) which would then select every second template within, which I don't want. I want it to select every second of a certain class (or in my case ID).
CHEWX is offline   Reply With Quote
Old 11-14-2012, 11:17 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
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
Quote:
Originally Posted by CHEWX View Post
Ok, regardless of the illegal ID use. This wouldn't work then ?

As I would be using #main :nth-child(2n) which would then select every second template within, which I don't want. I want it to select every second of a certain class (or in my case ID).
Code:
#article-inspir:nth-child(2n)
hits both
Code:
<article id=​"article-inspir" class=​"drop-shadow curved curved-hz-1">

i will say however, that you shouldn't actually need to use scripts for this.
simply use CSS to add clear:both to the same selector, or use :after content if you really need to inject a styled block.



in general with these sort of problems, just try out different permutations in the firebug or dev tools console until you hit the tags you want.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 11-14-2012, 12:06 PM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Quote:
Originally Posted by CHEWX View Post
I want it to select every second of a certain class (or in my case ID).
YES, you can do it for "class"

NO, it won't work for "id"
devnull69 is offline   Reply With Quote
Old 11-14-2012, 07:04 PM   PM User | #6
CHEWX
Regular Coder

 
Join Date: Dec 2010
Posts: 124
Thanks: 17
Thanked 6 Times in 6 Posts
CHEWX is an unknown quantity at this point
Quote:
Originally Posted by devnull69 View Post
YES, you can do it for "class"

NO, it won't work for "id"
Really, so if I was to change the ID to a class and add this code

Code:
$(document).ready(function() {
$(".article-inspir:nth-child(2n)").append("<div style='clear:both;'></div>");
});
It would would ? Doesn't that rubbish what was said above ?

Quote:
Originally Posted by rnd me View Post
Code:
#article-inspir:nth-child(2n)
hits both
Code:
<article id=​"article-inspir" class=​"drop-shadow curved curved-hz-1">

i will say however, that you shouldn't actually need to use scripts for this.
simply use CSS to add clear:both to the same selector, or use :after content if you really need to inject a styled block.



in general with these sort of problems, just try out different permutations in the firebug or dev tools console until you hit the tags you want.
There is nothing in CSS that will work with this. Only nth term which isn't backwards compatible so that is why I'm using Jquery.
CHEWX is offline   Reply With Quote
Old 11-14-2012, 07:23 PM   PM User | #7
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Quote:
Doesn't that rubbish what was said above ?
Indeed ... sorry
devnull69 is offline   Reply With Quote
Old 11-14-2012, 08:28 PM   PM User | #8
CHEWX
Regular Coder

 
Join Date: Dec 2010
Posts: 124
Thanks: 17
Thanked 6 Times in 6 Posts
CHEWX is an unknown quantity at this point
It doesn't work.

I've change to class's and it's not selecting. It's the only way to fix the problem. I've manually inputted the clear after every second and it fixes the problem, but I need to use javascript to append and no worky.

What's the problem ?
CHEWX is offline   Reply With Quote
Old 11-14-2012, 08:31 PM   PM User | #9
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
It works perfectly in this example, so the problem must be somewhere else

Wait wait wait ... you said "manually inputted the clear after every second" ... that's not what your code is doing

.append() will append a new CHILD (sub node) and not a new SIBLING (node after the current node). Try .after() instead

Code:
$(document).ready(function() {
   $(".article-inspir:nth-child(2n)").after("<div style='clear:both;'></div>");
});

Last edited by devnull69; 11-14-2012 at 08:36 PM..
devnull69 is offline   Reply With Quote
Users who have thanked devnull69 for this post:
CHEWX (11-14-2012)
Old 11-14-2012, 08:36 PM   PM User | #10
CHEWX
Regular Coder

 
Join Date: Dec 2010
Posts: 124
Thanks: 17
Thanked 6 Times in 6 Posts
CHEWX is an unknown quantity at this point
Oh, thanks for that. Yeah it does work.

As you can see it's adding the XXX to the correct class.

But the clear doesn't.

I usually add the clear AFTER the div in the code, rather that TO the div. What would I need to do to get the jquery to add the code AFTER the div, rather than TO the div. If that makes sense.
CHEWX is offline   Reply With Quote
Old 11-14-2012, 08:40 PM   PM User | #11
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
see latest edit above
devnull69 is offline   Reply With Quote
Old 11-14-2012, 08:44 PM   PM User | #12
CHEWX
Regular Coder

 
Join Date: Dec 2010
Posts: 124
Thanks: 17
Thanked 6 Times in 6 Posts
CHEWX is an unknown quantity at this point
AHHH ! I SEE.

Thanks so much, legend.

Works perfectly.
CHEWX 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:26 PM.


Advertisement
Log in to turn off these ads.