Object doesn't support property or method "find" in jquery

I have links below in my form

<link type="text/css" rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

      

I am trying to find the content of the title element from the line below.

var mytext = "<head><title>bad error</title></head>";
var err = mytext.find('title'); 

      

I am getting error Object doesn't support property or method 'find'

.

By my requirement I want to get the text between the element <title>

.

+3


source to share


3 answers


Use the filter tag



$(mytext).filter('title').text();

 will give you the title tag text.

+1


source


find

is a jquery method, not a method string

. Wrap your HTML string in jquery.

Use text

to get the innerText of an element.

$(mytext).find('title').text();

      



Example:

var mytext = "<head><title>bad error</title></head>";
var err = $(mytext).find('title').text();

alert(err);

      

+3


source


The problem is what mytext

is the string. Strings do not have a search method. You want to instantiate jQuery and use it. If your string really looks like you sent it, the jQuery parsing result will already be a header element, and all you have to do is read the text:

var mytext = $("<head><title>bad error</title></head>");
var err = mytext.text();

alert(err);
      

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      

Run codeHide result


+1


source







All Articles