Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

In my webpage I have two places where I need my JavaScript to write dates. How to do it in one go?

Selectors:

var $today = $('#today');
var $selected = $('#selected');

If I were to do this alternatively the easy way, I'd do:

$('#today, #selected').html(date);

But I kind of need to reuse my #selected jQuery object throughout my code.

The only solution I came up with so far is...

$([$today, $selected]).each(function(idx, obj){obj.html(date);});

Is there a better way?

share|improve this question

1 Answer 1

up vote 9 down vote accepted

I believe you would be better off combining the two jQuery objects using .add() rather than a comma in the selector. It's also more elegant than having to call .each().

$today.add($selected).html(date);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.