Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Enhanced attr(String, Function) to pass through index of current element
  • Loading branch information
jzaefferer committed Jan 16, 2007
1 parent 8b51bfb commit c8b7881
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/jquery/coreTest.js
Expand Up @@ -82,8 +82,9 @@ test("attr(String)", function() {
});

test("attr(String, Function)", function() {
expect(1);
expect(2);
ok( $('#text1').attr('value', function() { return this.id })[0].value == "text1", "Set value from id" );
ok( $('#text1').attr('title', function(i) { return i }).attr('title') == "0", "Set value with an index");
});

test("attr(Hash)", function() {
Expand Down
14 changes: 10 additions & 4 deletions src/jquery/jquery.js
Expand Up @@ -411,10 +411,16 @@ jQuery.fn = jQuery.prototype = {
* @result <img src="test.jpg" title="test.jpg" />
* @desc Sets title attribute from src attribute.
*
* @example $("img").attr("title", function(index) { return this.title + (i + 1); });
* @before <img title="pic" /><img title="pic" /><img title="pic" />
* @result <img title="pic1" /><img title="pic2" /><img title="pic3" />
* @desc Enumerate title attribute.
*
* @name attr
* @type jQuery
* @param String key The name of the property to set.
* @param Function value A function returning the value to set.
* Scope: Current element, argument: Index of current element
* @cat DOM/Attributes
*/
attr: function( key, value, type ) {
Expand All @@ -430,12 +436,12 @@ jQuery.fn = jQuery.prototype = {
}

// Check to see if we're setting style values
return this.each(function(){
return this.each(function(index){
// Set all the styles
for ( var prop in obj )
jQuery.attr(
type ? this.style : this,
prop, jQuery.prop(this, obj[prop], type)
prop, jQuery.prop(this, obj[prop], type, index)
);
});
},
Expand Down Expand Up @@ -1256,10 +1262,10 @@ jQuery.extend({
return obj;
},

prop: function(elem, value, type){
prop: function(elem, value, type, index){
// Handle executable functions
if ( jQuery.isFunction( value ) )
return value.call( elem );
return value.call( elem, [index] );

// Handle passing in a number to a CSS property
if ( value.constructor == Number && type == "curCSS" )
Expand Down

0 comments on commit c8b7881

Please sign in to comment.