Link to home
Start Free TrialLog in
Avatar of jazzIIIlove
jazzIIIloveFlag for Sweden

asked on

grabbing a href in query.

hi there;

I have a problem as grabbing the active tab to do some operations.
I have 3 tabs which are practically links

<a href="#tabs-0">My rolls</a>
<a href="#tabs-1">Your balls</a>
<a href="#tabs-2">His arms</a>

what i need is to find out active link (clicked link). I already wrote a click function but failed to differentiate the hrefs as i will need to populate my tab with the clicked tab/link.

Any fast short cut?

Regards.
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Test page : http://jsfiddle.net/67jUr/
$(document).ready(function() {

    $("a", "#tabs").click(function() {
        var href = $(this).attr("href");
        alert( "link in tabs clicked : " + href );
    })
    
    // IF YOUR LINKS ARE ADDED DYNAMICALLY USE jQuery.on
    $("#dynamics-tabs").on("click", "a", function() {        
        var href = $(this).attr("href");
        alert( "link in dynamics-tabs clicked : " + href );
    })
})

Open in new window

Avatar of jazzIIIlove

ASKER

Hi;

Yours is working perfectly but i couldn't adapt it properly to mine..

<li class="ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
<a href="#tabs-0">My balls</a></li>

<li class="ui-state-default ui-corner-top">
<a href="#tabs-1">Your balls</a></li>

Open in new window


The issue is that when a li gets active(a tab selected, its class adds the value ui-state-active to its li as above.

I reached the relevant tab; <a href="#tabs-1"> by appending an index tabs-index but couldn't find clicked so that i can populate that tab.

regards.
could you set a test page at jsfiddle?
ah, i used your other solution (since you are versatile in your solutions) and works fine for now.

One irrelevant question. you know, in the other question, it was the div who is parent and my question is that does this propogate to children (this <a href is a child) just as that?

regards.
no the event propagate from child to parent
but initialy you bind the click event to the <a href
Ok things got messy and i Also added ids to code which is now:
<a id='tabs-0' href...>
There are my parent lis for each href just above, and append a class to my li like active as follows:

<li class="c1 c2 c3 iamfucked" so iamfucked appears class only when i click that tab and i need that lis child href value.

I went for if($("li").hasClass("iamfucked))..but cannot get the matching href with find()...

Sorry for this question..

Thanks.
to get the href from the li you need to find the link

var href = $("li.iamfucked").find("a").attr("href");
i tried but i cannot grab the value...

I also tried...

var href = $("li.c1c2.c3.iamfucked").find("a").attr("href");

I also think the if check which is to find the li is working but maybe it is not finding the li (the parent of href):
  if($("li").has("c1.c2.c3.iamfucked"))
            {
var href = $("li.c1c2.c3.iamfucked").find("a").attr("href");

if i go for hasClass it is false so i used has so that may be the problem.

Regards.
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi

I find out that i grab the click event before rendering the page..

It works in chrome but not as console.log

I have to think upon it.
Leakim is a life savior :)