Is it possible to use the jquery attribute contains selector on $(this)
? I cannot find any examples. I am trying to see if input fields contain a certain word.
$("#form input").keyup(function() {
if ($(this).filter( "[name~='someWord']" )) {
console.log('yes');
}
});
The code returns yes for all input even if they don't contain someWord
.
Is it possible to use the jquery attribute contains selector on $(this)
? I cannot find any examples. I am trying to see if input fields contain a certain word.
$("#form input").keyup(function() {
if ($(this).filter( "[name~='someWord']" )) {
console.log('yes');
}
});
The code returns yes for all input even if they don't contain someWord
.
-
1
The result of
.filter
is always a jQuery object wrapping 0 or more elements. It is always a truthy value, regardless of whether the filter returns 0 elements or not. – user229044 ♦ Commented Mar 21, 2016 at 16:41 -
2
What's wrong with
$("#form input[name~='someWord']").keyup(handler);
??? EDIT: just rereading your question, your posted code seems plelty unrelevant to your expected behaviour, you don't want to check for name attribute but for input value instead (property, not attribute) – A. Wolff Commented Mar 21, 2016 at 16:52 -
@CyberJunkie Is requirement of Question to include call to
jQuery()
with parameterthis
:$(this)
andjQuery( "[attribute~='value']" )
selector api.jquery./attribute-contains-word-selector withinif
condition ? "The code returns yes for all input even if they don't containsomeWord
." Can you create stacksnippets to demonstrate ? – guest271314 Commented Mar 21, 2016 at 17:31 - I'm not going to play a devil's advocate, but there is a small "war" between Neal and RajaprabhuAravindasamy (Raja for now) going here. I suggest to not look to the vote amount of the answer, but the answer itself. I have used Raja's fiddle (he placed that in his ment somewhere in this post) to show a parision of both answers. updated fiddle example with answer from Neal & Raja. Please use the console (F12 on browser) to check the output and determine for yourself which answer is right one. – KarelG Commented Mar 21, 2016 at 17:37
-
@KarelG in all cases Raja return
No
and for all mine returnyes
. interesting. – Naftali Commented Mar 21, 2016 at 18:52
3 Answers
Reset to default 5You have to use .is()
at this context,
if($(this).is( "[name~='someWord']" )) {
console.log('yes');
}
Because .filter()
would return a jquery object (element collection), and that would never be false
.
You can do this even better with Vanilla JS in the event handler
$("#form input").keyup(function(evt) {
if (evt.currentTarget.name.indexOf('someWord') > -1) {
console.log('yes');
}
});
This checks the name
attribute of the actual DOM element and does not add the overhead of the jquery wrapper.
Try
$("#form input").keyup(function() {
if ($(this).filter("[name~='someWord']").length) {
console.log('yes');
}
});
or better:
$("#form input").keyup(function() {
if ($(this).is("[name~='someWord']")) {
console.log('yes');
}
});