Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
am using dropdown to view all sheetname .i want to print all sheetname in radio button instead of dropdown. How can i do that in JavaScript?

What I have tried:

JavaScript
$(document).ready(function() {
var a = {
	            Sheets: [{
	                "sheetID": "0",
	                "sheetname": "sheet1"
	            }, {
	            	"sheetID": "1",
	                "sheetname": "sheet2"
	            }, {
	            	"sheetID": "2",
	                "sheetname": "sheet3"
	            }, {
	            	"sheetID": "3",
	                "sheetname": "sheet4"
	            }]
	        };
	       $.each(a.Sheets, function (key, value) {$("#sheet").append($('<option').val(value.sheetID).html(value.sheetname));
	        });

	        $('#sheet').change(function () {
	            alert($(this).val());
	            //Code to select image based on selected car id
	        });
Posted
Updated 24-Oct-17 2:55am
v3

1 solution

Although not a part of the answer, but that is not JSON. That is just a plain JavaScript object. It is funny that JSON document and a JavaScript object are quite the same thing (but, still, naming them correctly can help you get valid and useful answers).

Now for the answer part, you can just change the code where you are generating the option elements and use the radio buttons instead,
JavaScript
$.each(a.Sheets, function (key, value) 
{
    $("#sheet").append("<input type='radio' name='" + value.sheetname + "' /> " + value.sheetID + " <br />");
});

Something like this will help you out. As for other styling or DOM purposes, add more elements such as the <label> etc.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900