Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Jquery Selectors on $.AJAX loaded HTML?

Tags:

jquery

I have

$.ajax({   url: identity,   success: function(data) { ProcessIdentityServer(data) } }); 

When 'data' is returned, is there a way to run selectors against it without adding it into the DOM. So for example, how can I get all the href values of any LINK tags contained in the HTML held in 'data' without adding it to the DOM first? Seems a shame to have to add it into the DOM if all I want to do is extract some stuff into an array. Anyone got any ideas?

like image 817
jdee Avatar asked Jan 01 '09 20:01

jdee


People also ask

Can jQuery and Ajax be used together?

jQuery provides several methods for AJAX functionality. With the jQuery AJAX methods, you can request text, HTML, XML, or JSON from a remote server using both HTTP Get and HTTP Post - And you can load the external data directly into the selected HTML elements of your web page!

How jQuery selectors are executed?

jQuery selector: jQuery selectors are used to selecting the HTML element(s) and allow you to manipulate the HTML element(s) in a way we want. It selects the HTML elements on a variable parameter such as their name, classes, id, types, attributes, attribute values, etc.

Can we use HTTP GET or POST for Ajax calls?

HTTP Request: GET vs. POSTGET is basically used for just getting (retrieving) some data from the server. Note: The GET method may return cached data. POST can also be used to get some data from the server. However, the POST method NEVER caches data, and is often used to send data along with the request.

Does jQuery provide its own selectors?

jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more. It's based on the existing CSS Selectors, and in addition, it has some own custom selectors.


1 Answers

One note I will add which is from a similar problem on here is that if your AJAX returns the following:

<div class="test">Hello</div> <div class="one">World</div> 

The following jQuery Won't work:

$(data).find('div.test'); 

as the divs are top level elements and data isn't an element but a string, to make it work you need to use .filter

$(data).filter('div.test'); 
like image 115
stuartloxton Avatar answered Sep 26 '22 19:09

stuartloxton