2

I have below HTML format:

<ul class="whatlike-list">
    <li class="first">
        <a href="/SessionHandler.aspx" class="button-flight-search">Search for Flights</a>
        <div class="open-block-holder">
            <div id="slideFlightSearch" class="open-block" style="display: block; left: 650px;">

            </div>
        </div>
    </li>
</ul>

Now I am looking to get DIV ID="slideFlightSearch" on the click of link "Search for Flights", I have got the class "button-flight-search" in my $this object. Something like below in my JQuery.

$(link).click(function()
{
    alert($(this).attr("class"));
})

In the above alert I am getting the class "button-flight-search", however I need the inner DIV ID slideFlightSearch

Please suggest using JQuery.

3 Answers 3

2

You can use .next and .find, like this:

$(link).click(function() {
    var id = $(this).next(".open-block-holder")
                    .find("div") // or .find("div.open-block")
                    .attr("id");
});
Sign up to request clarification or add additional context in comments.

7 Comments

Isn't the question about getting to #slideFlightSearch w/o knowing the ID? If we know the id, we could just skip immediately to $('#slideFlightSearch').doSomething();
No I need just the div ID, which will be passed in my code for furthur use
@David Hedlund - Thank you for the input. Fixed.
@MKS: Depending on your structure, you could add .find('div:first') to only get the first div (or any other way that gives you the first only).
.attr('id') will always just yield the ID of the first element in the set, regardless of whether there were more that were matched by the selector...
|
1

You could try:

$('a').click(
    function(){
        var theDivID = $(this).closest('li').find('.open-block').attr('id');
        alert('The div's ID is: ' + theDivID);
        return false; // If you need to prevent the default action of clicking on a link.
    });

Comments

0

A method using your HTML structure without using ID or class attributes.

$(link).click(function()
{
    alert($(this).next()         //get div with class "open-block-holder".
                 .children()     //get the inner div with class "open-block".
                 .attr("id") ) ; //get the ID.

})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.