[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Selecting an element with jquery

Posted on 2011-10-10
7
Medium Priority
?
236 Views
Last Modified: 2012-06-27
Hi

I have an html  page with attached code in. When i click the anchor link (id = "click6") i want to select the div that is directly previous to it . (the one that has display none)

How can i do that with jquery?

Thanks
<div style="width:590px; float:right; margin-top:32px;">
    <p>content...</p>
    
    <div style="display: none">
     <p>Content<p>
    </div>
    <p><a id="click6" name="click">EXPAND BIO</a>.</p>

Open in new window

0
Comment
Question by:coolispaul
  • 3
  • 2
  • 2
7 Comments
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36941774
following code will highlight that div when the link is clicked

$("a#click6").bind("click", function(){

 $(this).prev("div").css("border", "2px solid red")
});
0
 
LVL 15

Accepted Solution

by:
Eyal earned 1336 total points
ID: 36941777
.parent().prev()
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 664 total points
ID: 36941789
sorry it should be


$("a#click6").bind("click", function(){

 $(this).parent().prev("div").css("border", "2px solid red")
});
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:coolispaul
ID: 36941794
great yes it works with parent()

Why doesnt  $(this).prev("div") work? thats actually what i tried..

Cheers
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 36941798
because anchor is inside the <p> and is not at the same level as div
0
 
LVL 15

Assisted Solution

by:Eyal
Eyal earned 1336 total points
ID: 36941799
you have a p and inside it you have the encore
the div is sibling of the p and not the encore
0
 

Author Comment

by:coolispaul
ID: 36941813
oh ok i thought .prev(div) just looked for the previous "div" element in the HTML regardless of being sibling or not.

Ok thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

834 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