2

I have a setup basically like this:

<div id="container">
    <div class="field_shell">
        <label for="name">Name</label>
        <input type="text" name="name" id="name" class="field">
    </div> 
    <div class="field_shell">
        <label for="age">Age</label>
        <input type="text" name="age" id="name" class="field"> 
    </div>
</div>

Except on a larger scale, there are three sets of #container, each #container holding several (and varying amounts of) div.field_shell elements. Most div.field_shell hold only one input, but a few do hold two separate input elements.

My issue is how I can count the number of input elements in each #content. Bonus points for giving some insight on the best method of checking if each input in each #container div is filled, and returning specific results if so.

I assume .length is viable here, but I am stumped. Especially on checking the "status" of each #container (by status, I mean if each input within that #container is filled, and not empty).

Thank you guys so much for reading through, and thank you more for helping :)

2
  • In your html i dont saw #content Commented Jan 30, 2014 at 7:30
  • You might have gotten the length of the input element, which is 0 if unfilled. Commented Jan 30, 2014 at 7:39

4 Answers 4

6

I'm assuming you want to use jQuery, I've changed your HTML code a bit to make it work. Ids should be unique.

<div id="container">
    <div class="field_shell">
        <label for="name">Name</label>
        <input type="text" name="name" id="name" class="field"/>
    </div>
    <div class="field_shell">
        <label for="age">Age</label>
        <input type="text" name="age" id="age" class="field"/>
    </div>
</div>

and the jQuery code to count for each #container, It will traverse the DOM tree and return all elements that match input

var inputs = $("#container").find($("input") );
console.log(inputs.length)

If you want to check for filled input elements, you can look at the element.length property. To count for multiple containers, you could run a loop to handle each #container and nest the code above in it.

Sign up to request clarification or add additional context in comments.

Comments

0

try this code,

    <script>
    $(document).ready(function () {
        var FieldShellelements = $("#container .field_shell");
        for (i = 0; i < FieldShellelements.length; i++) {
            alert($(FieldShellelements[i]).children('input').length);
        }
    });
</script>

Comments

0

Demo FIDDLE

I hope this is what you want

HTML

    <div id="container">

 <div class="field_shell">

   <label for="name">Name</label>
   <input type="text" name="name" id="name" value="demo" class="field">

 </div>

 <div class="field_shell">

   <label for="age">Age</label>
   <input type="text" name="age" id="name" class="field">

 </div>

</div>

Jquery

    var count=0;
$('#container').find('input[type="text"]').each(function(){
    if ($.trim($(this).val()).length) {
        count+=1
        alert("Filled Value="+$(this).val());
    }
});
alert("Total Input Count="+$('#container').find('input[type="text"]').length+"//Filled Inputs Count="+count);

Comments

0

You should try to keeps IDs unique on a page, since $("#container") will only ever match the first instance (whereas $("[id='container']") will pick up all of them).

// Gather all containers that have all inputs filled
var allInputsFilled = $("[id='container']").filter(function () {
        var allFilled = true,
            inputs = $(this).find("input");

        inputs.each(function () {
            // !val() equates to true when the value is empty
            // '' is a falsy value, inverted with ! not operator
            if (!$(this).val()) {
                allFilled = false;
                return false; // break out of loop early
            }
        });

        return allFilled; 
    });

allInputsFilled will contain all #container divs that have all their inputs filled. Demo.

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.