Reputation: 7969
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
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
Reputation: 21130
Try this.
$('button').click(function(){
var name = $("#name");
if(name.val() != 'jitender'){
alert("not valid name");
name.val('');
}
});
});
Upvotes: 1
Reputation: 165
You can simply call this after your alert - $('#name').val('newValue');
See here - http://api.jquery.com/val/
Upvotes: 0
Reputation: 10003
you can simply add
if(name!='jitender'){
alert("not valid name");
$('#name').val(''); //here comes clearing input
}
Upvotes: 1
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