Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use jQuery on a variable instead on the DOM ?

In jQuery you can do :

$("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
}

I want to write a jQuery function which crawls x-levels from a website and collects all hrefs to gif images.

So when I use the the get function to retrieve another page,

$.get(href, function(data) {

});

I want to be able to do something like

data.$("a[href$='.img']").each(function(index) {
});

Is this possible ?

...UPDATE...

Thanks to the answers, I was able to fix this problem.

function FetchPage(href) {
    $.ajax({
        url: href,
        async: false,
        cache: false,
        success: function(html){
            $("#__tmp__").append("<page><name>" + href + "</name><content>" + html + "</content></page>");
        }
    });
}

See this zip file for an example how to use it.

like image 551
Stef Heyenrath Avatar asked May 07 '10 06:05

Stef Heyenrath


People also ask

Is jQuery better than DOM?

Pure JavaScript can be faster for DOM selection/manipulation than jQuery as JavaScript is directly processed by the browser. jQuery has to be converted into JavaScript to make it run in a browser. All these can be done in JavaScript but we may have to write many lines of code.

Can I use variable in jQuery selector?

Projects In JavaScript & JQueryYes, it is possible to pass a variable into a jQuery attribute-contains selector. The [attribute*=value] selector is used to select each element with a specific attribute and a value containing a string.

How do you use jQuery to select a specific DOM node?

If you have a variable containing a DOM element, and want to select elements related to that DOM element, simply wrap it in a jQuery object. var myDomElement = document. getElementById( "foo" ); // A plain DOM element.

Is jQuery same as DOM?

A jQuery object is a Javascript object, which may or may not have anything to do with the DOM (usually it does). A jQuery object is a convenience wrapper around a DOM element in Javascript which is a method to manipulate the DOM which is a representation of the page which was created from an HTML file.


1 Answers

you could put recived data into the DOM and then run

$("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
}

something like this

$.get(href, function(data) {
  $("#somelement").hide();
  $("#somelement").html(data);
  $("#somelement").find("a[href$='.img']").each(function(index) {
    alert($(this).attr('href'));
  }
  $("#somelement").show();
}
like image 146
Maksim Burnin Avatar answered Sep 21 '22 03:09

Maksim Burnin