5

I have a jQuery ajax get that will return an html text. From this one I need to extract the h3 element (with id='title') value: THIS IS TITLE.

Can I achieve this with jQuery?

<div class="content">
   <h3 id="title">THIS IS TITLE</h3>
   .....
</div>

and here is the call:

  $.ajax({
      url: url,
      type: 'GET',
      cache:false,
      success: function (data) { // data is the html text returned

       alert('The title is: ' + TITLE HERE);

      }
  });
3
  • Maybe using find I guess ? Commented Jul 24, 2014 at 6:53
  • 3
    $(data).find('#title').text() Commented Jul 24, 2014 at 6:53
  • Since Id is unique, you can use $('#title').text() Commented Jul 24, 2014 at 6:54

2 Answers 2

8

Use find() method to get the value like below,

$(data).find('#title').text()

An Example for how to use find is here How to Use find()

Sign up to request clarification or add additional context in comments.

Comments

2

Use RegExp:

text='<div class="content"><h3 id="title">THIS IS TITLE</h3>.....</div>';
console.log(text.match(/<h3 id="title">(.*?)<\/h3>/)[1]);// or alert:
alert('The title is: ' + text.match(/<h3 id="title">(.*?)<\/h3>/)[1]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.