views:

56

answers:

3

Hello,

I have the following HTML:

<ul actualpage=0>
<li/>
<li/>
....
</ul>

<ul actualpage=0>
<li/>
<li/>
....
</ul>

Im trying to get the value of actualpage of each ul and create a new attribute. Its easy but not in one jquery sentence... Its possible? Until now i have the following line (between ## simbols the missing part that i need.

    /*
select all uls with attribute actualpage and create a new attribute on each with the current actualpage value
    */

$('ul[actualpage]').attr('newactualpage',##Current value of actualpage attr of UL##);
+2  A: 

Well maybe this isn't as nice as you'd like, but

$('ul[actualpage]').each(function(_, ul) { $(ul).attr('newactualpage', $(ul).attr('actualpage')); });
Pointy
+1  A: 

One might think that

$('ul[actualpage]').attr('newactualpage',$(this).attr('actualpage'))

is the answer.

However, this is evaluated before the call to attr, so it's going to equal whatever this equals in the calling context.

You could use:

$('ul[actualpage]').attr('newactualpage',function(){
    return $(this).attr('actualpage');
});

or this:

$('ul[actualpage]').each(function()
{
     $(this).attr('newactualpage',$(this).attr('actualpage'));
};

In both, this refers to the element that your selector matched.

Dancrumb
Well, also keep in mind that the "this" you reference in the *argument list* to the "attr" function will not have anything to do with the selected list of `<ul>` elements. It'd be whatever "this" references in the context of the jQuery expression itself.
Pointy
Re-reading this, I have to mark it down as being simply wrong. Regardless of whether "actualpage" varies from tag to tag, the first solution just will not work.
Pointy
The first-only limit of .attr() applies only for getting the attribute, not setting it, right?
jholster
@Pointy; appreciate you taking the time to check the validity of my solution, but I don't agree with your assessment. If you are using the `$` function with the Selector context, `this` refers to a bare DOM element that was selected by your Selector. This is indicated in the documentation here: http://api.jquery.com/jQuery/#jQuery1That said, if I've missed your point or your interpretation is different, please let me know; I'd rather learn something new than be dogmatic about being right.
Dancrumb
@Yaggo; you are correct.
Dancrumb
Sorry @Dancrumb, but you're incorrect. It's easy enough to try it and see. Just think about how the jQuery expression has to be evaluated by the interpreter: when you put "this" in an argument to a function, that has to be evaluated *before* the function is called. That is, the entire chain of "function().function().function()..." is prepared in the calling context, *before* any "this" assignments in the actual called functions.
Pointy
@Pointy is correct about the behaviour of `this` in the _first code block_. @Dancrumb, I think you are misinterpreting Pointy's answer. First example doesn't work not because of the reason you give, but because of wrong use of `this`.
jholster
@Pointy; thanks... i wrote a test document and saw that `this` was the document. I'd been thinking of `attr` as an alias, of sorts, to the `each` function, when applied to the Selector context. I've modified the answer accordingly. Thanks again.
Dancrumb
@Dancrumb no problem - Javascript is almost endlessly weird for something that seems as simple as it does! I learn something really surprising here on SO just about every day.
Pointy
That's so true. Recommended reading: http://javascript.crockford.com/
jholster
+1  A: 

You can use function as second argument for .attr(), eliminating the need of .each():

$('ul[actualpage]').attr('newactualpage', function() { return $(this).attr('actualpage') });
jholster