Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery val() contains()

Tags:

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')){...
like image 889
Hussein Avatar asked Feb 08 '11 22:02

Hussein


People also ask

What does VAL () do in jQuery?

val() method is primarily used to get the values of form elements such as input , select and textarea . When called on an empty collection, it returns undefined .

How use contains in jQuery?

jQuery :contains() SelectorThe :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).

How do I find a word in a string using jQuery?

How to find if a word or a substring is present in the given string. In this case, we will use the includes() method which determines whether a string contains the specified word or a substring. If the word or substring is present in the given string, the includes() method returns true; otherwise, it returns false.


2 Answers

Try javascript

if (value.indexOf('iframe') >= 0) {

JQuery contains is for DOM elements, not strings.

like image 119
Nikita Rybak Avatar answered Jan 03 '23 01:01

Nikita Rybak


Try doing it like this:

$('#embedModal textarea:contains("iframe")').each(function() {
  //Do something
});

edit

Example

like image 30
Aaron Hathaway Avatar answered Jan 03 '23 00:01

Aaron Hathaway