kasey
kasey

Reputation: 185

How to dynamically add a unique id to a radio button

I am dynamically adding radio buttons to html through js. I need to add unique id's to the radio buttons so I can access the value selected. How do I do it?

here is the js code.

 function createMyElements(standardAddress,k){
            return [
                    '<input type="radio" id=',"selectedAddress[k]",' name="addressField"  value=>', standardAddress[k],'<br>',
        ].join('\n');
        }

where selectedAddress is an array with some constants to identify the radio button selected.

Upvotes: 0

Views: 1230

Answers (1)

gurvinder372
gurvinder372

Reputation: 68393

change the method to

 function createMyElements(standardAddress,k){
            return [
                    '<input type="radio" id=',"selectedAddress["+k+"]",' name="addressField"  value=>', standardAddress[k],'<br>',
        ].join('\n');
        }

"selectedAddress[k]" is changed to "selectedAddress["+k+"]" to append the value of k to the "selectedAddress"

Upvotes: 3

Related Questions