Solved

Jquery: find any paragraph that is followed by an unordered list, then apply CSS rule

Posted on 2011-09-14
3
377 Views
Last Modified: 2012-05-12
At the moment i have a css rule that applies a padding-bottom:13px; to all paragraphs.
Unfortunately, this gap doesn't look good when an unordered list immediately follows a paragraph.

So, I would like to apply a margin-top:-13px; to the unordered list.

I need jquery code that:

1. Searches inside a div content.
2. In this div content, find any paragraph that is followed by an unordered list, apply margin-top:-10px to the unordered list

I know the Jquery code to find the paragraph and apply css rule goes something like this I think,
$('p').css( "margin-top", "-13px" );


Sample HTML Code

 
<div class="content">
<p>This is the first paragraph</p>
<p>This is the second paragraph</p>

<p>This is a title </p>
<ul>
  <li>the quick brown fox</li>
  <li>jumped over </li>
  <li>the lazy sheep</li>
</ul>

<p>This is another title </p>
<ul>
  <li>the lazy sheep</li>
  <li>jumped over </li>
  <li>the quick brown fox</li>
</ul>
</div>

Open in new window


Thanks!
0
Comment
Question by:jeremyll
3 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 100 total points
ID: 36540405
For example : $("p", "div").next("ul").css( "margin-top", "-13px" );

Test page : http://jsfiddle.net/94sFf/
0
 
LVL 82

Accepted Solution

by:
hielo earned 400 total points
ID: 36540417
>>At the moment i have a css rule that applies a padding-bottom:13px; to all paragraphs.
How about applying NO padding to P preceeding UL:
<script type="text/javascript">
$(function(){
	$('ul').prev('p').css( "margin-bottom", "0px" );
});
</script>

but if you want the apply it to the ul, try:
$('p + ul').css('margin-top','-13px');

Open in new window

0
 

Author Closing Comment

by:jeremyll
ID: 36540476
You guys are Legendary!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
specific questions about shipping a coat 4 39
jquery datepciker end date add 6 days 6 34
jqury 17 28
Select case on click 3 13
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

785 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