Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6559471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:19:52+00:00 2026-05-25T13:19:52+00:00

Suppose my javascript called server(AJAX call) and then got server response with an array

  • 0

Suppose my javascript called server(AJAX call) and then got server response with an array of data : dataArray.

the number of data in the dataArray will be either 1 or 2 or 3, that’s no more than 3.

I would like to show the data as radio button selections. Since the number of data is dynamic(1 or 2 or 3), so I should show radio buttons dynamically.

I came up with two ways to do this:

1st way: have three radio buttons in the HTML:

<div id="my-radio-btns">
   <input id="data0" type="radio" name="datas" value="0"><label for="data0"></label>
   <input id="data1" type="radio" name="datas" value="1"><label for="data1"></label>
   <input id="data2" type="radio" name="datas" value="2"><label for="data2"></label>
</div>

then, hide and show certain number of the above radio buttons based on the number of data in the dataArray.

2nd way: use javascript to append radio button HTML strings:

for(var i=0; i<dataArray.length; i++){

     $('#my-radio-btns').append("<input id='data"+i+"type='radio' name='datas' value="+i+"> <label for='data"+i+"></label>");

}

I am wondering, which way is better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:19:53+00:00Added an answer on May 25, 2026 at 1:19 pm

    As you already have a couple of solutions it really comes down to a discussion on the merits of each.

    With the first approach:

    • You don’t have to create any markup as it’s already there.
    • If you are hiding these elements then you can use display:none or
      visibility:hidden, the latter will reserve the space in the page
      layout such that there is no shifting of elements as you display them.
    • If your number of expected elements increase, what’s so bad about adding another radio
      button to the markup?

    The second approach:

    • Allows you to simply increase the number of radio buttons displayed by increasing the number of data elements in the array.
    • It probably won’t sway your desicion much, as it looks like your DOM
      manipulation is quite minimal, but one of the most expensive
      operations in jquery is modifying the DOM. Just something to think about.

    Also, may I suggest a little enhancement to the second code sample:

    var markup = "";
    for(var i=0; i<dataArray.length; i++){
        markup += "<input id='data"+i+"type='radio' name='datas' value="+i+"> <label for='data"+i+"></label>";
    }
    
    $('#my-radio-btns').append(markup);
    

    Prevents multiple lookups of the my-radio-btns element – won’t make a huge amount of difference over the space of 3 elements but if you increase this is may start of add up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a Javascript array, like so: var test = ['b', 'c', 'd',
Suppose you want to make an async request in JavaScript, but you want to
Since Actionscript is a proper superset of javascript, it should I suppose be possible.
This is about inheritance in JavaScript. Suppose I create a constructor Bird(), and another
Consider the following Javascript function (1): function setData(domElement) { domElement.myDataProperty = { 'suppose': 'this',
Suppose I'm entertaining a business model that offers free access to my propritary data
Suppose I have this code : <head> <script type=text/javascript> func1(); </script> <script type=text/javascript src=jquery.js></script>
Suppose in Javascript that you assign the SRC to an IMG tag. It is
Suppose one line or few lines of javascript is assigned to a variable. How
I'm still relatively new to javascript and jQuery and was just wondering this. Suppose

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.