2

I want to hide all those rows whose fields are empty using jquery.

Here is my Code DEMO: http://jsfiddle.net/7k3y3/19/

HTML:

<div class="container">

    <div id="row_3">
        <input type="text" id="field_3" name="field[]" value="PHP"/>
    </div>  

    <div id="row_5">
        <input type="text" id="field_5" name="field[]" value="Javascript"/>
    </div>  


    <div id="row_8">
        <input type="text" id="field_8" name="field[]" value=""/>
    </div>  


    <div id="row_10">
        <input type="text" id="field_10" name="field[]" value="C++"/>
    </div>  


    <div id="row_12">
        <input type="text" id="field_12" name="field[]" value=""/>
    </div>      

</div>

<a href="javascript:void(0);" class="save">SAVE</a>

JS :

$(".save").click(function ()
    {
        var fieldArr = $( "input[name='field[]']" );

        for(i=1; i<=fieldArr.length;i++)
        {

            if(fieldArr[i]=='')
            {
                fieldArr[i].hide();    
            }

        }



    });
1
  • Use class instead of name in your HTML code. Commented Jan 8, 2014 at 5:52

4 Answers 4

2

Write:

$(".save").click(function (){
    $("input[name='field[]']").each(function(){
        if($(this).val() == ""){
            $(this).hide();
        }
    });
});

Updated fiddle here.

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

Comments

1
$(".save").click(function (){
    $('.container :input').each(function() {
        if ($(this).val().length == 0) {
            $(this).hide();
        }
    });                    
});

The demo.

Comments

1

This will work. Have tested it on fiddle.

$(".save").click(function(){
    $( "input[name='field[]" ).each(function(){ 
        if($(this).val() == ''){
            $(this).hide();
        }
    });              
});

3 Comments

EXACTLY COPIED FROM PREVIOUS ANSWER, First Posted 10mins before !
@BetaCoder He has tested it :)
@BetaCoder By the time I had run the code on fiddle and submitted here, Exactly similar answer was already posted :) Not my fault.
1

You are comparing a DOM Element object with an empty string which is not true, of course, you should read the .value properties instead. Also you are calling jQuery .hide() method on a DOM element object, you should either use jQuery .eq(i) method which returns a jQuery wrapped element or manipulate the style property.

for (var i = 0; i < fieldArr.length; i++) {
    if (fieldArr[i].value == '') {
        fieldArr[i].parentNode.style.display = 'none';
    }
}

Since you are using jQuery you can also use the .filter() method, which acts like a loop but only returns the elements that meet the condition, in this case values with length of 0:

$('.container > div').filter(function() {
    return !$("input[name='field[]']", this).val().length; 
}).hide();

http://jsfiddle.net/GLeZc/

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.