1

I am burned out on this (may be due to Friday), I tried, googled but didn't find a way to make this thing work. the code looks straight forward, but it doesn't add the radio buttons to my div. I am sure, i am missing something silly.

var m_fastingOptions = [{ "Id": "1", "Name": "Fasting" }, { "Id": "2", "Name": "Not Fasting" }];

$.each(m_fastingOptions, function() {
    $("#dvFastingOptions").append(
        $('<label />', {
            'text': this.FastingName                            
        }).append(
            $('<input />', { 
                type: 'radio', 
                name: 'rdoFastingOptions', 
                id: 'rdoFastingOptions' + this.Id,
                value: this.Id
            } )
        )
    );    
});

Here is the jsFiddle that I was playing with. Any help is much appreciated.

After adding the correct jQuery version to jsFiddle, I see radio buttons but not the label text.

I need to see: O Fasting O Not Fasting

7
  • 2
    Your fiddle link isn't working :) Commented Jul 26, 2013 at 19:58
  • Try including jquery. jsfiddle.net/DsQFn ... always check your console. You would see that $ is undefined. Commented Jul 26, 2013 at 19:59
  • youre missing jQuery in your fiddle. after adding that its working :) Commented Jul 26, 2013 at 19:59
  • you need to add jquery on the jsFiddle Commented Jul 26, 2013 at 20:00
  • First step when something isn't working: Check your console. You would have immediately seen "Uncaught ReferenceError: $ is not defined" and known that jQuery wasn't referenced. Commented Jul 26, 2013 at 20:01

2 Answers 2

3

The problem is that, you are referring to the wrong property name.

Instead of Name, you are trying to navigate to FastingName

$('<label />', {
    'text': this.Name //This is correct
})

Either that, or you update your object to be:

var m_fastingOptions = [{
    "Id": "1",
    "FastingName": "Fasting"
}, {
    "Id": "2",
    "FastingName": "Not Fasting"
}];

See fiddle here: http://jsfiddle.net/pratik136/XWHsA/

Edit:

To see the output as

O Fasting O Not Fasting

use .prepend() instead of .append()

Sign up to request clarification or add additional context in comments.

2 Comments

Not my day today. It is one of the worst question I have ever asked on SO. I need to go home now. :((
that is so cool,but if I need to take an ID of a particular checkbox then ` $('input:radio[name=rdoFastingOptions]:checked').each(function(){var selectedValue=$(this).val()} ` use this
1

Finally i found solution to the problem, here is the answer code,

var listOfOptions = [{ "Id": "forYourself", "Name": "For Yourself" }, { "Id": "myEmployees", "Name": "My Employees" }, { "Id": "forOthers", "Name": "For Others" }];
        $.each(listOfOptions, function() {
            $(".optionDiv").append(
                $('<input />', {
                    type: 'radio',
                    name: 'rdoOptions',
                    id: 'rdoOptions' + this.Id
                })
            );
            $(".optionDiv").append(
                        $('<label />', {
                            'text': this.Name
                        })
            )

        });
    });

Any better simpller way to do this?

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.