Muthukamatchi Ganesan
Muthukamatchi Ganesan

Reputation: 373

Checkbox cannot display in html page

I am building a html page using dom concept, but the checkbox is not displaying in the html page. My code is below.

if (condition <= 0) {
 $doc = $(document.createElement("li")).attr({
     id: id,
     name: name,
     'class': 'liBullet'
 })
}

else {
 $doc = $(document.createElement("ul")).attr({
     id: id,
     name: name,
     'class': 'liBullet'
 })
}

$doc.append(
    $(document.createElement("input")).attr({
     id: id,
     type: "checkbox",
     name: name,
     'class': 'layerCheck'
    })

    .append(
        $(document.createElement("label")).attr({
          id: id,
          name: name
        })
    ).text(name)

    .append(
        $(document.createElement("div")).attr({
         id: id,
         'class': 'loadingLegend'
        })
    )).appendTo("#layer_list");
}

Upvotes: 0

Views: 111

Answers (2)

Mahmoud Farahat
Mahmoud Farahat

Reputation: 5475

you can also use this :

$("<input type='checkbox' id='id' name='name' value='val'/>").appendTo("body");

or

$("body").append("<input type='checkbox' id='id' name='name' value='val'/>");

read about append & appendTo

Upvotes: 0

karim79
karim79

Reputation: 342625

$(document.createElement("input")).attr({
                                     id: layer.id,
                                     type: "checkbox",
                                     name: layer.name, // <---- missing comma?
                                     'class': 'layerCheck'
                                 }); // <-- or missing semicolon?

Also, you don't need to use createElement, you can just do:

$("<input />").attr({
                       id: layer.id,
                     type: "checkbox",
                     name: layer.name,
                  'class': 'layerCheck'
              }).appendTo("body");

Upvotes: 3

Related Questions