user1886368
user1886368

Reputation: 21

JQuery .each Against a Variable

How does one specify a target variable with the jquery each function? I am performing a get request and placing the results in a variable. I want to run the each function on that variable to pull out all the id tags. Many thanks.

$.get("somepage.php?id=45 #my_target_div", function(data) {
   $.each(function(data, "#id") {
   });
});

Upvotes: 0

Views: 75

Answers (2)

Kevin B
Kevin B

Reputation: 95023

The $.get() method doesn't allow you to filter down the results with a selector like .load() does, and your use of .each() is wrong.

This would allow you to loop through all elements that have an id attribute inside the my_target_div.

$.get("somepage.php?id=45", function(data) {    
   $("<div />").html(data).find("#my_target_div [id]").each(function(){
       // annoying alert
       alert(this.id);
       // or the more appealing console log
       // console.log(this.id);
   });
});

Upvotes: 3

gen_Eric
gen_Eric

Reputation: 227230

$.each takes 2 parameters. The object you want to loop over, and a callback to call on each element.

$.each(data, function(key, value){
});

P.S. The syntax you are using for the URL doesn't work with $.get. It only works with $().load.

Upvotes: 4

Related Questions