SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2007
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Link scope in jQuery

    I know this is really simple, but it always seems to stump me.

    Say I have several divs that each have a certain link. How can I make sure that when a link is clicked that it will only change elements that are within it's parent div and not the others?

  2. #2
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say you have this structure:
    Code HTML4Strict:
    <div class="fun">
    <a href="..."></a>
    <p>Howdy!</p>
    </div>
     
    <div class="fun">
    <a href="..."></a>
    <p>Howdy!</p>
    </div>
     
    <div class="fun">
    <a href="..."></a>
    <p>Howdy!</p>
    </div>

    Then, if you want to manipulate the paragraph element within the same div as the link you clicked, you would use:

    Code JavaScript:
    $(".fun>a").click(function(){
          $(this).siblings("p").css('color','#ccc');
    });

    Is this what you meant?

    P.S. - Example uses Jquery

    Cheers.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •