Saturday, August 1, 2009

[jQuery] Re: How can I select an element's parent?

you should change your html something like:
<ul id="product_links">
<li id="first"><a href="#">FIRST</a></li>
<li id="seacond"><a href="#">SECOND</a></li>
<li id="third"><a href="#">THIRD</a></li>
</ul>
then in order to get the id of your li you can use the following code
$('a', '#product_links').click(function() {
alert($(this).parent().attr('id'));
});


On Aug 1, 11:39 am, macsig <sigbac...@gmail.com> wrote:
> Hello guys,
> I'm trying to update a script I found out there in order to fit better
> my needs.
>
> The first step I want to update is the capability to bind a dynamic
> number of anchors.
> Right now with the HTML below
>
> <ul id="product_links">
>    <li class="first" ><a>FIRST></a></li>
>    <li class="second">SECOND</a></li>
>    <li class="third"><a>THIRD</a></li>
> </ul>
>
> I have the script below:
>
>  $("#product_links .first a ").bind("click", function(){ pupup_element
> ("first"); });
>  $("#product_links .second a ").bind("click", function(){ pupup_element
> ("second");});
>  $("#product_links .third a ").bind("click", function(){ pupup_element
> ("third");});
>
> It works fine but since the number of anchors changes dynamically
> (through RoR) I'd like to have just 1 line that binds all the anchors.
> So here 2 questions for you:
>
> - how can I select an element's parent?
> - Does the code below work?
>
>   $("#product_links a ").bind("click", function(){ pupup_elemet
> ("this.PARENT.id");});
>
> basically I want to bind all the anchors within the product_links list
> to popup_element function that takes as a parameter the ID of the
> anchor parent. In my case the id of the list item.
>
> Thanks and have a nice weekend.
>
> Sig

No comments: