1

I am trying use JQquery to add custom radio buttons to a parent element:

function createRadio(name, parentID, values) {
  var parent = $('#' + parentId);
  for (var i = 0; i < values.length; i++) {
      $('<input />', {
        type: 'radio',
        name: name,
        value: values[i]
    }).appendTo(parent);
    $('<label />', {
        'for': 'cb',
        text: values[i]
    }).appendTo(parent);
  }
}

However, this code works:

    $(document).ready(function () {
        $('<input type="radio" name="rad" value="a">').appendTo('#radios');
        $('<input type="radio" name="rad" value="b">').appendTo('#radios');
        $('<input type="radio" name="rad" value="b">').appendTo('#radios');
    });

But this doesn't:

    $(document).ready(function () {
        createRadio('rad', 'radios', ['a', 'b', 'c']);
    });

The HTML has a div who id is radios:

 <div id="radios">Radios</div>

Thanks for your time.

1
  • Please provide the definition to the parent variable. Commented Oct 5, 2014 at 14:31

1 Answer 1

2

Your code is correct but you've mistaken in spellings parentID and parentId.

Check spelling of parentID in both function declaration (function createRadio(name, parentID, values)) parameters and var parent = $('#' + parentId);

It should be,

function createRadio(name, parentID, values) {
var parent = $('#' + parentID);
.....
.....
.....
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.