Johan
Johan

Reputation: 35213

Mapping values to a certain class

 $.each(data, function (i, val) {
                        if ($('input:text').hasClass(i)) {
                            $('.' + i).val(val);
                        }

It works, but im not a big fan of the $('.' + i).val(val);. Must be a smarter way than combining strings using jquery, right?

Upvotes: 1

Views: 47

Answers (2)

leepowers
leepowers

Reputation: 38318

Build selectors via string concatenation isn't a big deal. However using two selectors when one will work is wasteful:

$.each(data, function(i, val) {
  $('INPUT.' + i).val(val); 
});

Upvotes: 1

Russell
Russell

Reputation: 17739

We use similar to that to describe classes. You could combine the two:

$("input:text ." + i).val(val);

Upvotes: 0

Related Questions