Avatar of coolispaul
coolispaulFlag for United States of America

asked on 

Selecting an element with jquery

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

JavaScriptWeb Languages and Standards

Avatar of undefined
Last Comment
coolispaul
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

following code will highlight that div when the link is clicked

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

 $(this).prev("div").css("border", "2px solid red")
});
ASKER CERTIFIED SOLUTION
Avatar of Eyal
Eyal
Flag of Israel image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of coolispaul
coolispaul
Flag of United States of America image

ASKER

great yes it works with parent()

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

Cheers
because anchor is inside the <p> and is not at the same level as div
SOLUTION
Avatar of Eyal
Eyal
Flag of Israel image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of coolispaul
coolispaul
Flag of United States of America image

ASKER

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

Ok thanks
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo