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

Example

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
}
Share:
60,192
Hussein
Author by

Hussein

#Proficiency .in{ XHTML:CSS; } jQuery( '#Addict' ) <? echo "PHP Advanced"; ?> Mastery in Photoshop / Illustrator Contact me

Updated on July 09, 2022

Comments

  • Hussein
    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
    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 its length property. See this fiddle vs. this one.
  • Ram Lakhan Yadav
    Ram Lakhan Yadav over 11 years
    yes, 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
    Shadow The Kid Wizard over 11 years
    No, it will never be false because jQuery collection is an object.
  • Jason Foglia
    Jason Foglia about 11 years
    This won't work in all IE browsers. developer.mozilla.org/en-US/docs/JavaScript/Reference/…
  • Nikita Rybak
    Nikita Rybak about 11 years
    @JasonFoglia That is the documentation for the array's indexOf function, not string's indexOf.