Quoting through text fields using jquery

How can I clear these text fields by going through them using jQuery?

<div class="col-md-4" id="RegexInsert">
    <h4>New Regex Pattern</h4>
    <form role="form">
        <div class="form-group">
            <label for="firstName">Desription</label>
            <input type="text" class="form-control" id="txtRegexDesription" placeholder="Description" />
        </div>
        <div class="form-group">
            <label for="firstName">Regex pattern</label>
            <input type="text" class="form-control" id="txtRegexPattern" placeholder="Regex pattern(C#)" />
        </div>
        <div class="form-group">
            <label for="firstName">Data type</label>
            <input type="text" class="form-control" id="txtDataType" placeholder="Data type" />
        </div>
        <button type="button" class="btn btn-default btn-sm btnAddRegexPattern" data-applicationid=""><span class="glyphicon glyphicon-plus"></span>&nbsp;Add</button>
    </form>
</div>

I am doing the following, which is not yet.

$("#RegexInsert .inputs").each(function () {
    $('input').val('');
});

Yours faithfully

+4
source share
6 answers

You do not need a loop, you can do this in one selector:

$('.form-control').val('');

I am not sure where .inputsin your example it goes, as it is not in your HTML. You can make the selector more general if required:

$('#RegexInsert input[type="text"]').val('');
+11
source

To clear input elements as indicated elsewhere:

$("#RegexInsert .inputs").val('');

In reset the values ​​of the input elements:

$("#RegexInsert .inputs").val(function(){
    return this.defaultValue;
});

Or, if you have several types of input (and this, frankly, is more complicated than I initially expected it to be ...):

$('#reset').on('click', function(e){
    e.preventDefault();
    $('input, textarea, select option')
    .each(function(){
        var tag = this.tagName.toLowerCase(),
            type = this.type,
            prop, value;
        if ('undefined' !== typeof type) {
            switch (type) {
                case 'radio':
                case 'checkbox':
                    prop = 'checked';
                    break;
                case 'text':
                case 'textarea': // textarea elements seem to have a type
                                 // property, undexpectedly (in Chrome/Win XP)
                    prop = 'value';
                    break;
            }
        }
        else {
            switch (tag) {
                case 'option':
                    prop = 'selected';
                    break;
                case 'textarea':    // left this just in case the 'type' property
                    prop = 'value'; // is a Chrome/Webkit specific thing.
                    break;
            }
        }
        this[prop] = this['default' + prop.replace(/^./,function(a){
            return a.toUpperCase();
        })];
    });
});

JS Fiddle demo.

+6

$("#RegexInsert input").val('');
+4

:

$("#RegexInsert input").each(function () {
    $(this).val(); // Gets the value
    $(this).val('newValue'); // Sets the value
});

(() ):

$("#RegexInsert input").val('');

Beware, as this will clear up any input that has a value attribute. You might want to consider a stronger selector, for example$("#RegexInsert input[type=text]")

+4
source

follow these steps:

$('#RegexInsert input.form-control').val('');

or

$('.form-group input.form-control').val('');
+3
source

your source code

$("#RegexInsert .inputs").each(function () {
    $('input').val('');
});

.Inputs does not exist anywhere in your HTML. If you are referencing input tags, your input should be input.

$("#RegexInsert input").each(function () {
    $(this).val('');
});
0
source

Source: https://habr.com/ru/post/1539870/


All Articles