Skip to main content
deleted 82 characters in body; edited tags; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Algebra & jQuery - How to shorten this jquery loop function Set of radio buttons

How would you shorten this code?

I wrote a quick function to create a set of radio buttons, three per table row.

But i I would like some feedback from the jquery community about how toI can shorten this bit of code using algebraic functions, or different jqueryjQuery functionality.

var arr = [ "earth", "mars", "jupiter", "saturn", "venus", "argus", "pluto", "janus", "canary", "orange", "butter pineapple", "eventual bliss"]; var obj = { one:"earth", two:"mars", three:"jupiter", four:"saturn", five:"venus" }; var start = ""; end = ""; var count = 0; $("#radio_container").append("<table>"); $.each(arr, function(i, val) { if(count == 3) { start = "<tr>"; end = "</tr>" count= 0; } $("#radio_container").append(start); $("#radio_container").append("<td><input name='finalcategory' type='radio' value='"+val+"'></input></td><td class='style3'>"+val+"</td>"); $("#radio_container").append(end); count++; start = ""; end = ""; }); $("#radio_container").append("</table>"); 

Algebra & jQuery - How to shorten this jquery loop function

How would you shorten this code?

I wrote a quick function to create a set of radio buttons, three per table row.

But i would like some feedback from the jquery community about how to shorten this bit of code using algebraic functions, or different jquery functionality.

var arr = [ "earth", "mars", "jupiter", "saturn", "venus", "argus", "pluto", "janus", "canary", "orange", "butter pineapple", "eventual bliss"]; var obj = { one:"earth", two:"mars", three:"jupiter", four:"saturn", five:"venus" }; var start = ""; end = ""; var count = 0; $("#radio_container").append("<table>"); $.each(arr, function(i, val) { if(count == 3) { start = "<tr>"; end = "</tr>" count= 0; } $("#radio_container").append(start); $("#radio_container").append("<td><input name='finalcategory' type='radio' value='"+val+"'></input></td><td class='style3'>"+val+"</td>"); $("#radio_container").append(end); count++; start = ""; end = ""; }); $("#radio_container").append("</table>"); 

Set of radio buttons

I wrote a quick function to create a set of radio buttons, three per table row. I would like some feedback about how I can shorten this bit of code using algebraic functions, or different jQuery functionality.

var arr = [ "earth", "mars", "jupiter", "saturn", "venus", "argus", "pluto", "janus", "canary", "orange", "butter pineapple", "eventual bliss"]; var obj = { one:"earth", two:"mars", three:"jupiter", four:"saturn", five:"venus" }; var start = ""; end = ""; var count = 0; $("#radio_container").append("<table>"); $.each(arr, function(i, val) { if(count == 3) { start = "<tr>"; end = "</tr>" count= 0; } $("#radio_container").append(start); $("#radio_container").append("<td><input name='finalcategory' type='radio' value='"+val+"'></input></td><td class='style3'>"+val+"</td>"); $("#radio_container").append(end); count++; start = ""; end = ""; }); $("#radio_container").append("</table>"); 
Tweeted twitter.com/#!/StackCodeReview/status/233702101925109760
Post Migrated Here from stackoverflow.com (revisions)
Source Link
Tony H.
  • 133
  • 2

Algebra & jQuery - How to shorten this jquery loop function

How would you shorten this code?

I wrote a quick function to create a set of radio buttons, three per table row.

But i would like some feedback from the jquery community about how to shorten this bit of code using algebraic functions, or different jquery functionality.

var arr = [ "earth", "mars", "jupiter", "saturn", "venus", "argus", "pluto", "janus", "canary", "orange", "butter pineapple", "eventual bliss"]; var obj = { one:"earth", two:"mars", three:"jupiter", four:"saturn", five:"venus" }; var start = ""; end = ""; var count = 0; $("#radio_container").append("<table>"); $.each(arr, function(i, val) { if(count == 3) { start = "<tr>"; end = "</tr>" count= 0; } $("#radio_container").append(start); $("#radio_container").append("<td><input name='finalcategory' type='radio' value='"+val+"'></input></td><td class='style3'>"+val+"</td>"); $("#radio_container").append(end); count++; start = ""; end = ""; }); $("#radio_container").append("</table>"); ​