Lakmini
Lakmini

Reputation: 13

How to know whether an element has another element inside inside its class

<html>
<head>
<script>
function A(){
    $('input[name="B[]"]').each(function() { 
        if(('$(this) .BtnSet .Child input:text[name="A[]"]').length){
         //yes the Child class is inside JRow class and has textboxes with name A[]
        }
    });
    return false;
}
</script>
</head>
<body>
<form onsubmit="return A()">
    <div class="row JRow">
     <input type="text" name="B[]"></input>
     <input type="text" name="B[]"></input>
        <div class="BtnSet">
            <div class="Child">
                <input type="text" name="A[]"></input>
                <input type="text" name="A[]"></input>
            </div>
        </div>
    </div>
    <input type="submit" value="Submit"></input>
</form>
</body>
</html>

I need to check whether B[] has a BtnSet class. Inside it, whether there is a Child class and inside it whether there are children elements like A[]. Have I done it precisely? But the problem is in this case when I print alert('$(this) .BtnSet .Child input:text[name="A[]"]').length) the lenth is coming always as 45. Please explain me the reason for that ? Doesnt it gives the correct length as 2?

Upvotes: 0

Views: 107

Answers (3)

Vineeth Pradhan
Vineeth Pradhan

Reputation: 8371

I think the number 45 is the length of the string $(this) .BtnSet .Child input:text[name="A[]"] which you obviously don't require. What you need is the length of the two input elements which is within the div.BtnSet. So this should work

 $(this).siblings("div.BtnSet").first().find("input[name='A[]']").length;

Upvotes: 0

user2587132
user2587132

Reputation:

$(this).find("*[class='BtnSet']").length;

you are getting the string length, $(this) should be outside, and like @CoursesWeb pointed out these are not the children of the input elements with name=b[] but are siblings, you will have to use siblings for that!

 $(this).siblings("*[class='BtnSet']").length;

Upvotes: 2

CoursesWeb
CoursesWeb

Reputation: 4237

BtnSet is not insside B[], but is a child in row jRow, like B[].

Text Input tags has no child items.

Upvotes: 0

Related Questions