Joseph
Joseph

Reputation: 119827

Select parent containing specific child

i have this structure in html:

<ul id="resultlist">
    <li>
        <input type="hidden" value="idNumber" />
        <dl>
        ...
        </dl>
    </li>
</ul>

is there a one-liner method in jquery that finds the <li> based on the child?

currently i have this in my code to find the <li> but it's quite long:

//dynamic value
var idNumber = 2; 

//finds input with value of 2
var hiddenInput = $('#resultList').find('input[value='+idNumber+']'); 

var listItem = hiddenInput.parent();

Upvotes: 0

Views: 82

Answers (1)

user1106925
user1106925

Reputation:

If you want a one-liner, just chain the calls.

var listItem = $('#resultList input[value="'+idNumber+'"]').parent();

I also brought the input selector into the initial one to shorten it up a bit, and I also included the missing quotation marks around the attribute value.


You could use the non-standard :has() selector I suppose if you really want it...

var listItem = $('#resultList li:has(input[value="'+idNumber+'"])');

Upvotes: 3

Related Questions