1

Is it possible to write a Javascript function to delete form a field when somebody does not fill in the field?

<form id="myform">
<label for="q1" id="q1label">question 1</label>
<input type="text" id="q1" name="q1"/>
<label for="q2" id="q2label">question 2</label>
<input type="text" id="q2" name="q2"/>
<label for="q3" id="q3label">question 3</label>
<input type="text" id="q3" name="q3"/>
<input type="submit" value="Delete blank fields" onclick="return checkanddelete"/>
</form>

If somebody does not fill in question 2 for example, it deletes question 2 label and the field.

1
  • You mean, you do not want the form to be submitted but instead to have some fields deleted when the submit button is pressed? Commented May 16, 2013 at 11:32

4 Answers 4

3

For jQuery:

<script type="text/javascript">
function checkanddelete() {
    $('input').each(function(){
        if ($(this).val() == '') {
            var id = $(this).attr('id');
            $.remove('#' + id);
            $.remove('#' + id + 'label');
        }
    });
}
</script>

For JavaScript:

<script type="text/javascript">
function checkanddelete() {
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++)
{
    if (document.getElementsByTagName("input")[i].value.length == 0) {
        var id = document.getElementsByTagName("input")[i].id;
        (elem=document.getElementById(id)).parentNode.removeChild(elem);
        (elem=document.getElementById(id + 'label')).parentNode.removeChild(elem)
    }
}
}
</script>

Something like this?

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

5 Comments

Won't that give a reference error about $ not being defined?
Thx for your reply. I will have a try.
user1667419, please reload as i've added plain JavaScript instead of jQuery :) (nnnnn, you too haha)
that will loop over every input field in the page, not that pretty. some event binding would be nice to
Willem, the javascript #2 should be in a function, same as the jquery #1; if done it's in an event. It's not that pretty, that's true, but for reference it should be enough.
1

With jquery:

$("#myform :text").each(function(){
    if( !$.trim($(this).val()) )
        $(this).prev('label').andSelf().remove();
});

2 Comments

Please note that this only works with jQuery library, not plain-text JavaScript :)
Yeah you're right :) and that's why I added "With jquery" on top of my answer :D
0

i am using folloing function to remove element from document.

function removeElement(id)
{
    if(typeof id === "object")
        return id.parentNode.removeChild(id);
    else
        return (elem=document.getElementById(id)).parentNode.removeChild(elem);
}

You can pass a dom element or element Id itself to delete .

3 Comments

djeezes, is everyone here blind or what ? "when somebody does not fill in the field", read the question will you.
Willem D'haeseleer ofcourse conditions needs to be putted to check if the field is not filled by used thn call function to remove.
instead of putting that in a comment you should put it in your code
0

The following should do what you want :

var inputToDelete = document.getElementById("q2");
if (inputToDelete.value == "") {
   var labelToDelete = document.getElementById("q2label");
   var parentNode = document.getElementById("myform");
   parentNode.removeChild(labelToDelete);
   parentNode.removeChild(inputToDelete);
}

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.