I'm trying to clear the value of a input field if it contains a certain value.
$('#registerajax_email:contains("yahoo.com")').text(function(){
$('#registerajax_email').val('');
});
What am I doing wrong?
I'm trying to clear the value of a input field if it contains a certain value.
$('#registerajax_email:contains("yahoo.com")').text(function(){
$('#registerajax_email').val('');
});
What am I doing wrong?
The problem, I think, is that the value of an input field is contained inside the value attribute, not in the element's contents. You need an attribute selector.
Try this:
$('#registerajax_email[value*="yahoo.com"]').val('');
which uses the Attribute Contains Selector
This is the most generous of the jQuery attribute selectors that match against a value. It will select an element if the selector's string appears anywhere within the element's attribute value.
The selector you were using (the :contains() selector) does not look at element attributes:
The matching text can appear directly within the selected element, in any of that element's descendants, or a combination thereof.
However, since you're targeting the element by ID, you really don't need to use an attribute selector at all. Most likely this code should be contained inside an event, as Robert Koritnik suggested, and you can just check the string contains using a simple indexOf:
// Register event onBlur (you could also use change, or whatever event suited the situation)
$('#registerajax_email').blur(function() {
// Does value contain yahoo.com?
if ($(this).val().indexOf("yahoo.com") != -1)
{
// clear the value
$(this).val("");
}
});
Use an appropriate event handler such as .change() or .blur() or .load()
$('#registerajax_email[val*="yahoo.com"]').load(function() {
$(this).val('');
});
.load() is actually two different functions. I use it in this context: api.jquery.com/load-event You are confusing it with this: api.jquery.com/load "Note: The event handling suite also has a method named .load(). Which one is fired depends on the set of arguments passed."or: doing it only once in page lifecycle
When you want to clear an input box on page load if it contains a certain value. Then check whether your input has a certain value:
$(function(){
$('#registerajax_email[value*="yahoo.com"]').val('');
});
or even better, since it's an email
$(function(){
$('#registerajax_email[value$="yahoo.com"]').val('');
});
* means contains, $ means ends.
or: checking it every time after user enters something in the input
This input value is probably related to the user entering an email address. So the main problem with your code is: when your call jQuery function, your input doesn't contain the required value. You have to attach to blur event to check for the value.
$(function(){
$("#registerajax_email").blur(function(e){
var context = $(this); // store it because it's used multiple times
if (/yahoo\.com/i.test(context.val()))
{
context.val("");
}
});
});
Beware of the regular expression being used. because it's easier to check whether your input contains the required string.