Reputation: 11918
I have users that are putting in links to youtube videos and we want to only enable the button if the string (URL) they enter contains the word, 'youtube'. Below is the code and I can't get it to work how I need it to. Thanks...
var inputValue = $('#inputLink').val();
$('#submitBtn').attr("disabled", "disabled");
$('#inputLink').keyup(function(){
if (inputValue.toLowerCase().indexOf("youtube") >= 0) {
$('#submitBtn').removeAttr('disabled');
}
});
Upvotes: 1
Views: 884
Reputation: 126042
You need to reinitialize inputValue
each time:
$('#submitBtn').attr("disabled", "disabled");
$('#inputLink').keyup(function(){
var inputValue = this.value;
if (inputValue.toLowerCase().indexOf("youtube") >= 0) {
$('#submitBtn').removeAttr('disabled');
}
});
Example: http://jsfiddle.net/BGcDg/
Update: You could also do this with just one line if you wanted to:
var youtubeRegex = /youtube/i;
$('#inputLink').keyup(function(){
$("#submitBtn").attr("disabled", !youtubeRegex.test(this.value));
});
This will re-disable the field if the user removes "youtube" after entering it.
Example: http://jsfiddle.net/bdmhQ/
Upvotes: 2