Ivy
Ivy

Reputation: 2305

Get closest parent with stated attribute?

I have the following code :

<input pid="hidVoteKey" type="hidden" value="0" />
    <ul id="mainPostList" class="verticalList">
        @foreach (var postViewModel in Model.Posts)
        {
            <li><div class="voteCon">...</div></li>
        }
    </ul>

Then I have a jquery that loop all elements with class voteCon and then try to get the parent input like this :

$(".voteCon").each(function () {
     InitVoteControl($(this), $(this).parent("input[pid='hidVoteKey']").val());
 });

The problem is that it will not find the hiddenfield?

In this case the voteCon contains up/down buttons and there is some javascript functions bound here to make ajax calls. There will be multiple lists like the one above on the same page but thay all will have diffrent hidVoteKey.

Upvotes: 0

Views: 121

Answers (2)

ncn corp
ncn corp

Reputation: 111

Just use then not type hidden , you can use inline style="display:none;"

Upvotes: 0

Anthony Grist
Anthony Grist

Reputation: 38345

It won't find the <input> because it isn't a parent (or ancestor) of the <div class="voteCon">. It's on the same level as (a sibling of) the <ul>, which is an ancestor of the <div>. You could do this:

$(this).closest('ul').prev('input[pid="hidVoteKey"]').val()

Upvotes: 1

Related Questions