jquery val() contains()
60,192
Solution 1
Try javascript
if (value.indexOf('iframe') >= 0) {
JQuery contains is for DOM elements, not strings.
Solution 2
Try doing it like this:
$('#embedModal textarea:contains("iframe")').each(function() {
//Do something
});
edit
Solution 3
this works:
$.contains( document.documentElement, document.body ); // true
var babyEl = $('div#id');
var daddyEl = $('div#ID');
if($.contains(babyEl, daddyEl)) {
//do fun stuff
}
Author by
Hussein
#Proficiency .in{ XHTML:CSS; } jQuery( '#Addict' ) <? echo "PHP Advanced"; ?> Mastery in Photoshop / Illustrator Contact me
Updated on July 09, 2022Comments
-
Hussein almost 2 years
I want to know if the textarea value contains a certain word. This is not working for me.
var value = $('#embedModal textarea').val(); if($(value).contains('iframe')){...
-
Shadow The Kid Wizard over 11 years-1 because this was already suggested in this answer plus that
if
condition will always be true so it's pointless. jQuery selector will always return jQuery collection object, if you want to check it contains anything you should check itslength
property. See this fiddle vs. this one. -
Ram Lakhan Yadav over 11 yearsyes, jQuery selector will always return jQuery collection object but condition will be true at that point when any textarea contain iframe key world otherwise will be false ....
-
Shadow The Kid Wizard over 11 yearsNo, it will never be false because jQuery collection is an object.
-
Jason Foglia about 11 yearsThis won't work in all IE browsers. developer.mozilla.org/en-US/docs/JavaScript/Reference/…
-
Nikita Rybak about 11 years@JasonFoglia That is the documentation for the array's indexOf function, not string's indexOf.