0

thanks for helping... I'm creating a series of html elements in javascript, including checkboxes. I want to go ahead and mark one of the checkboxes by default. I cannot get the 'checked' property to exist using either pure javascript (checkbox.checked = true;) or jquery library (see below). See my example, thank you - EDIT: The pure jQuery solution does work, but I'm having trouble with a javascript solution. Subsequent checkbox elements seem to inhibit the 'checked' attribute on prior ones. See this fiddle for an example...fiddle example

function createToolbarElements(){
        //------topbar-------------
        var topbar = document.getElementById("topbar");
        topbar.innerHTML = "ZONE: ";

        //zone set
        ART.regions.unshift("All");
        ART.regions.push("osj");
        var numRegions = ART.regions.length;
        var region;
        for(i=0; i<numRegions; i+=1){
            region = ART.regions[i];
            var checkbox = document.createElement("input");
            checkbox.type = "checkbox";
            checkbox.name = region;
            checkbox.value = region;
            checkbox.id = "zone"+region;
            if(region === "All"){
                $("#zoneAll").prop("checked", true);
            }
            topbar.appendChild(checkbox);
            var label = document.createElement('label')
            label.htmlFor = region;
            label.appendChild(document.createTextNode(region));
            topbar.appendChild(label);
        }
}

here's the HTML:

    <div id="topbar" style="z-index: 2; position:absolute; height: 24px; padding: 4px;
    background-color: #DDD; color:#111;  font-family:'Trebuchet MS', Arial, Helvetica, sans-serif; 
    font-size:10pt; line-height:1.2">
    </div>
5
  • 1
    This question appears to be off-topic because it is about a typo (a missing #). Commented Sep 27, 2013 at 17:43
  • If you know which checkbox you want to check, why not wait until the end of the loop and call $("#zoneAll").prop("checked", true); or even document.getElementById('zoneAll').checked = true then instead of checking each iteration? Commented Sep 27, 2013 at 17:44
  • I tried both suggestions by Jason P, with no luck. Commented Sep 27, 2013 at 18:12
  • Could you please post your HTML? Commented Sep 27, 2013 at 18:12
  • I've added html... The pure jQuery solution does work, but I'm having trouble with a javascript solution. Subsequent checkbox elements seem to inhibit the 'checked' attribute on prior ones. See this fiddle for an example... fiddle Commented Sep 27, 2013 at 19:39

3 Answers 3

1

Why not just stick with jQuery, as you're using it anyway :

function createToolbarElements(){
        $('#topbar').html('ZONE: ');

        ART.regions.unshift('All');
        ART.regions.push('osj');

    var numRegions = ART.regions.length,
        region     = null;

    for(i=0; i<numRegions; i++){
        region = ART.regions[i];

        $('<input />', {
            type : 'checkbox',
            name : region,
            value: region,
            id   : 'zone' + region,
            checked: region == 'All'
        }).appendTo('#topbar');

        $('<label />', {
            'for': 'zone' + region,
            text : region
        }).appendTo('#topbar');
    }
}

FIDDLE

or not use jQuery at all, and just do:

checkbox.checked = region === "All";

Note that the element you're trying to grab from the DOM, $("#zoneAll") is actually appended to the DOM after you're trying to get it, so doing it that way will always fail as the element doesn't exist when you're trying to access it.

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

8 Comments

I tried reversing those instructions - still didn't have a checked attribute.
Did you check out the fiddle, it clearly works just fine as the checkboxes are checked ?
Yes, I just did. I got it to work, thank you. I'm new to jQuery as of today. I still don't understand why setting the checked attribute in the ways I tried did not work, though. I was not able to set the checked value at all.
I'm guessing it's because your selector would fail to get the element, as it wasn't appended when you where trying to access it, so it would always fail.
I was thinking that too, but I switched the append statement ahead of it, and it doesn't work. And I'm able to set other things, like name, value, type. I tried your non-jQuery suggestion as well, but didn't work... var checkbox = document.createElement("input"); checkbox.type = "checkbox"; checkbox.name = region; checkbox.value = region; checkbox.id = "zone"+region; topbar.appendChild(checkbox); checkbox.checked = region==="All";
|
1

You need # to select an element by id.

$("#zoneAll")

Comments

1

Use this:

$("#zoneAll").prop("checked", true);

Make sure your selector works. I assume you're trying to select by the ID zoneAll, in which case you need to use $('#zoneAll'). This will work in the case that your node looks like this in your HTML:

<input type="checkbox" id="zoneAll"></input>

EDIT: I originally said to use attr(), which was horrendously badwrong. After some Google-Fu, prop() is indeed the way to go.

3 Comments

+1 for the missing #, -1 for suggesting to use .attr() over .prop().
Ah, you're right. I'm stuck in the older days of jQuery. Thanks for the insight into prop().
The selector is correct. I've matched the resulting id ('zoneAll') to what I've typed, including with the '#' symbol $('#zoneAll').

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.