get an id of child element and store in a variable using jquery?

Posted by Simpson88Keys on Stack Overflow See other posts from Stack Overflow or by Simpson88Keys
Published on 2010-05-25T14:12:37Z Indexed on 2010/05/25 14:21 UTC
Read the original article Hit count: 186

Filed under:
|
|

I'm basically trying to do exactly what the subject suggests, but I'm getting "undefined" in my alert, and I'm not entirely sure why. I am fairly new to jquery, so, I probably have the syntax wrong, but not sure where to go from here. I'll post both of my attempts, which both yield "undefined" in the alert...

//In my first attempt, I'm trying to get the id of the inner a tag
<ul>
                <li id="l1" class="active"><a href="#c1">Samp 1</a></li>
                <li id="l2" class=""><a href="#c2">Samp 2</a></li>
                <li id="l3" class=""><a href="#c3">Samp 3</a></li>
        </ul>

var selected = $(".active).children("a").attr("id");
    alert(selected);

//In my second attempt, I'm trying to get the id of the currently selected li
    var selected = $(".active").attr("id");
    alert(selected);

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about attributes