Bananfisk
Bananfisk

Reputation: 41

Problem with jQueryscript selector

I try to write at jQuery-script who catch "Enter"-keypress and find the first A-tag who is on the same div with class "findMva" and who is not on an other child div with class "findMva".

Example 1:

Example 2

jQuery-script (who not is working on example 1, but not on example 2...):

$("input").keydown(function (e) {  
    if (e.which == 13) {  
        $(this).closest(".findMva").find(".mva").not(".findMva > .mva").first().click();  
    }  
}

HTML-markup:

<div class="findMva">
    <div class="findMva">
        <input type="text" id="input1" />
        <a class="mva" id="a1" href="#">More info 1...</a>
        <a class="mva" id="a2" href="#">More info 2...</a>
    </div>
    <input type="text" id="input2" />
    <div>
        <div>
            <div class="findMva">
                <input type="text" id="input3" />
                <a class="mva" id="a3" href="#">More info 3...</a>
                <a class="mva" id="a4" href="#">More info 4...</a>
                <div class="findMva">
                    <input type="text" id="input4" />
                    <a class="mva" id="a5" href="#">More info 5...</a>
                    <a class="mva" id="a6" href="#">More info 6...</a>
                    <div class="findMva">
                        <input type="text" id="input5" />
                        <a class="mva" id="a7" href="#">More info 7...</a>
                        <a class="mva" id="a8" href="#">More info 8...</a>
                    </div>
                </div>
            </div>
            <a class="mva" id="a9" href="#">More info 9...</a>
            <a class="mva" id="a10" href="#">More info 10...</a>
        </div>
    </div>
</div>

Any suggestions how to write the jQuery selector?

Upvotes: 1

Views: 107

Answers (1)

Bananfisk
Bananfisk

Reputation: 41

I found a solution:

$(document).ready(function () {
    $("input").keydown(function (e) {
        if (e.which == 13) {
          var o = $(this).closest(".findMva").clone();
            o.find(".findMva").remove();
          var myid = o.find(".mva").first().attr("id");
          $("#" + myid).focus();
        }
    });
});

In this case I'm dependent of an ID-attribute on the A-tag.

Upvotes: 2

Related Questions