Jitender
Jitender

Reputation: 7969

reset input type text using javascript?

i have small function that running on condintion, if text box value is not equal to "jitender"then alert will come up, but i also want reset the text box value if text box value is not equal to "jitender".

$(function(){
    $('button').click(function(){
        var name= $('#name').val();
        if(name!='jitender'){
            alert("not valid name")

            }

        });

    });


<input type="text"  id="name"/>

<button>click me</button>

Upvotes: 1

Views: 1857

Answers (5)

adedoy
adedoy

Reputation: 2273

$(function(){
    $('button').click(function(){
        if($('#name').val() !='jitender'){
                alert("not valid name")
                $('#name').val(""); // resets value
            }

        });

    });​

<input type="text"  id="name"/>
<button>click me</button>​

heres the link so you can test: Test the script

Upvotes: 1

Austin Brunkhorst
Austin Brunkhorst

Reputation: 21130

Try this.

$('button').click(function(){
        var name = $("#name");
        if(name.val() != 'jitender'){
            alert("not valid name");
            name.val('');
        }
    });
});

Upvotes: 1

nsearle
nsearle

Reputation: 165

You can simply call this after your alert - $('#name').val('newValue');

See here - http://api.jquery.com/val/

Upvotes: 0

shershen
shershen

Reputation: 10003

you can simply add

  if(name!='jitender'){
            alert("not valid name");
            $('#name').val(''); //here comes clearing input
            }

Upvotes: 1

tusar
tusar

Reputation: 3424

$(function(){
    $('button').click(function(){
        var name= $('#name').val();
        if(name!='jitender'){
            alert("not valid name")
                $('#name').val(""); // this will reset the value
            }

        });

    });


<input type="text"  id="name"/>

<button>click me</button>

Upvotes: 1

Related Questions