views:

25

answers:

1

This question is in continuation to How to get attributes of container in jquer, I have different containers on my webpage and all of them have <div id = "some values"> now how can I get attributes values separately for each component ?

Is there any way I can know which attribute id belong to which container div ?

Currently I am using :

var id = $( '.promotion' ).attr( 'id' );

But if I have multiple promotional components on page and all have same div attribute as id than how can I relate that this particular attribute id belonged to this specific container ?

Update: I am having a function which is called for each container present on the page and so if I am using above mentioned code than will it not always return me the first match for id in the div and would never go to other divs and so I will always get same value for id which is for the first container ? If so than what is the work around for this ?

var id = $( '.promotion' ).this.attr( 'id' );


var id = $( '.promotion' ).$this.attr( 'id' );

var id = this.$( '.promotion' ).attr( 'id' );

How would I know if the attribute value is for current container, so how should I use this properly to get this information ?

Hope this question is clear.

+1  A: 

You can loop through and process each div individually

$(".promotion").each(function() {
    var id = this.id; // 'this' is the html element, not the jquery object
});

Update

function myfunc() {
   alert(this);
}

// inside myfunc, this will be the window
myfunc(); 
// call (also: apply()) changes "this" to be the first argument
myfunc.call(document.getElementById("someid")); 

Jquery uses this to refer to the current element being processed. In events that would be the target element. In .each it is the current element in the collection.

Consider:

$(".promotion").click(function() { 
   alert(this); // will alert with the div that was clicked
});

In Jquery you can wrap any html element with a JQuery Object by using $(element). So when this is an html element like in the example above you can use $(this):

$(".promotion").click(function() { 
   alert($(this).attr("id")); // will alert with the div that was clicked
});

Play around with it here: http://jsbin.com/okuri3/edit.

More about this:

Joel Potter
But you can always use $(this) to get it as a jQuery object of course. +1
Hober
Please check my updates as it might clarify bit further of what actually I am trying to accomplish.
Rachel
@Hober: yup, if you need to. @Rachel: Can you post some more of your code? I don't understand what exactly you're trying to do from your update. If you already have a function that is called for every element, why can't you just call `element.id` or `$(element).attr("id")` to get the id of the specific element that was passed to the function?
Joel Potter
@Joel Potter: Updated my question with some code, hope it makes little more sense in here now.
Rachel
Basically I want to understand the use of `this` in jquery and make use of it in my question to make sure that attribute value am getting is always for the current container.
Rachel
@Joel Potter: I understand this explanation but what I am trying to see is that if I have multiple containers on the page and I have init function which is being called for each of the container and inside the init function am using `var id = $( '.promotion' ).attr( 'id' );` than how can I make sure that I am getting attribute value for the current container only and not other similar container, something like this.container.attr('id') but currently this gives an error...am not sure why...Am I making some sense here ?
Rachel
@Rachel. Please show some more markup. Particularly the init function, and how it is called.
Joel Potter