Branislav B.
Branislav B.

Reputation: 558

jQuery selecting element from string

I am working on an ASP.NET page. I am making AJAX requests when displaying some pages and it returns a string like '<html><head>......</head></html>'. I am setting this string as an inner html of an element to display content. All I want to do is select and append only one element and it contents from string. is it possible ? Sample:

string :

<html><head></head><body><div id='page'>CONTENTS ARE HERE </div></body></html>

element I want to select : <div id="page">CONTENTS ARE HERE </div>

Is there any way to do this by using jQuery or any other way ? thank you very much.

Upvotes: 1

Views: 232

Answers (2)

Felix Kling
Felix Kling

Reputation: 817238

You can do this easily with .load():

The .load() method, unlike $.get(), allows us to specify a portion of the remote document to be inserted. This is achieved with a special syntax for the url parameter. If one or more space characters are included in the string, the portion of the string following the first space is assumed to be a jQuery selector that determines the content to be loaded.

$('#target').load('<url here> #page');

Upvotes: 2

Kokos
Kokos

Reputation: 9121

Why not simply have the request only return what you want to output? It makes no sense to use jQuery to format the return output instead of doing this in the file that provides the output.

Upvotes: 0

Related Questions