1

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>

2 Answers 2

2
if ( $.trim( $( this ).val() ) == '' ) { $( this ).parent().remove() }
Sign up to request clarification or add additional context in comments.

1 Comment

+1 This would do the trick, except I'd probably cache first: var $this = $(this); since it's inside of a $.each .
1
$(‘input[value!=""]‘).val().length == 0 

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

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.