santa
santa

Reputation: 12512

Remove fields that contain no values with jQuery

This is a spin-off my earlier question. I have a set of form fields generated via a loop.

I also have a link below, that when clicked calls a jQuery function. The link opens a modal dialog which also has a form. I would like to DELETE the fields created via loop that do not contain any values when I submit the form from within the dialog.


EDIT:

After testing code I realized that because I submit my form the page actually gets refreshed and I use all values. So, I need to do the opposite -- collect all fields with values and after I submit my form I need to insert all those fields with values back to the page. Now I need help with the following code:

$("#myLink").click(function(){        

   $("#newForm").dialog({
        buttons: {
            "Submit": function() {
                $("myForm").submit();

                // BELOW is where I need help
                var fieldWithValue = "";

                if($('input[value != ""]').length > 0) {
                    $this = $(this);
                    fieldWithValue += $this.parent();
                }
                $("#vals").html(fieldWithValue);                    
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
   });
});

foreach ($arrays as $listValue) {
    echo '
    <div>
       <input type="text" maxlength="100" name="field[]">
    </div>';
}

<div id="vals"></div>
<a href="#" id="myLink">link</a>

Upvotes: 1

Views: 112

Answers (2)

AlexC
AlexC

Reputation: 9661

$(‘input[value!=""]‘).val().length == 0 

$('input:text[value=""]').parent().remove();

Upvotes: 1

George Cummins
George Cummins

Reputation: 28906

if ( $.trim( $( this ).val() ) == '' ) { $( this ).parent().remove() }

Upvotes: 2

Related Questions