javascript - Delete html element at submit - Stack Overflow

I'm new with JavaScript. Can someone give me an example how to delete an empty form element upon s

I'm new with JavaScript. Can someone give me an example how to delete an empty form element upon submit?

<form action='...' method='post' id='mySubmitForm'>
     <input type='text' name='name'>
     <input type='text' name='email'>
     <input type='text' name='phoneNumber'>
     <input type='submit' value='Save'>
</form>

Is there a easy way to check with JavaScript if the form is empty and delete it before submission if so?

I'm new with JavaScript. Can someone give me an example how to delete an empty form element upon submit?

<form action='...' method='post' id='mySubmitForm'>
     <input type='text' name='name'>
     <input type='text' name='email'>
     <input type='text' name='phoneNumber'>
     <input type='submit' value='Save'>
</form>

Is there a easy way to check with JavaScript if the form is empty and delete it before submission if so?

Share Improve this question asked Jun 23, 2011 at 18:24 hogni89hogni89 1,7196 gold badges22 silver badges41 bronze badges 1
  • what do you want specifically? do you want to disable the fields that are empty before the form submits? in most cases it makes more sense to figure out if a field is empty AFTER the form submission. with php/asp or what ever server side lang you are using to deal with the input. – Achshar Commented Jun 23, 2011 at 18:29
Add a ment  | 

5 Answers 5

Reset to default 2

There is a submit event that the browser throws before form submission that you can use.

reference: http://www.quirksmode/js/forms.html

Return false if you don't want the form to be submitted, true if you want it to happen. In the event, delete / add the extra inputs that you want accordingly.

function validate(formName)
{
    var form = document.forms[formName];

    //validate, and do stuff

    //remove items that you want with a call like this
    form.removeChild(document.getElementById(id));

    form.submit();
}

If this is for validation, you should really be doing validation server side, not client side.

You would call this function like so:

<input type=BUTTON onClick="validate('myForm')"/>

You can use jQuery, which is probably the easiest way.

$(document).ready(function() {
  $('#.mySubmitForm').submit(function(event) {
    event.preventDefault();

    $('input[type=text]').each(function() {
      var inputElement = $(this);

      inputElement.val() == "" ? inputElement.remove() : null;
    });

    $(this).trigger('submit');
  });
});

I didn't test that code, but it should delete the empty form values before submit, then remove them.

function onsubmit() {
    [].forEach.call(document.querySelectorAll('#mySubmitForm input[type=text]'), function(col) {
        if(col.value=='') col.disabled = 'disabled';
    });
}

and onsubmit="onsubmit()" in your <form> tag

Checking through javaScript is easy, but I'd advise you to have-and-assign an id attribute to your form elements

You can check in the following way,

var email = document.getElementById('email').value;

and you can remove email from your form as shown below

form.removeChild(document.getElementById('email'));
form.submit();

you can have a look at Adding and Removing HTML elements dynamically with Javascript for more details.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745522297a4631312.html

相关推荐

  • javascript - Delete html element at submit - Stack Overflow

    I'm new with JavaScript. Can someone give me an example how to delete an empty form element upon s

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信