javascript - How to simplify or dry syntax of this jquery code -


i'm doing same selection on whole bunch of radio button groups. thing changes name.

var fcolor = $(this).closest('.branch').find('input[name="fcolor"]:checked').val(); var bcolor = $(this).closest('.branch').find('input[name="bcolor"]:checked').val(); var sidec = $(this).closest('.branch').find('input[name="sidec"]:checked').val(); var linec = $(this).closest('.branch').find('input[name="linec"]:checked').val(); 

how simplify code i'm not repeating code this?

if you're interested in inputs name attribute, i'd select them all, create object of properties , values.

if need single out ones, give them common class, , select them that.

var props = {};  $(this).closest('.branch').find('input[name]:checked').each(function() {     props[ this.name ] = this.value; }); 

you'll end structure this:

props = {     fcolor: "some value",     bcolor: "some value",     sidec: "some value",     linec: "some value" }; 

...accessible as:

props.fcolor;  // "some value" 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -