gondolfier
gondolfier

Reputation: 221

Access element's id inside jQuery load()

I have a few divs that are empty at start and that I want to fill with content dynamically. I'd like to use jQuery for that. All divs start with the name "foo_" and then an individual identifier, as in "foo_a1". The id is to be used to find the right content for each div.

On my onLoad() function I have declared the following:

$("[id^=foo_]").load("folder/"+this.id);

Unfortunately, this.id is "undefined" in that scope. The same goes for

$(this)[0].id

How can I access the elements id inside the load function? I get the feeling it should be really easy but...

Some more info: The jQuery function html() accepts functions as the parameter and:

$("[id^=foo_]").html(function(){return this.id;});

does fill the divs with their corresponding ids. ( However: html(this.id) doesn't work. this.id is also "undefined" in that case).

Upvotes: 0

Views: 152

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388316

You can use .each() to iterate over the set of elements and then inside the each handler use this.id to access the target element's id

$("[id^=foo_]").each(function(){
    $(this).load("folder/"+this.id;});
});

Upvotes: 2

Related Questions