1

I'm trying to create a checkbox with Javascript and when it will be clicked on, time in format (hh:mm:ss) will display. The problem that I have is that it does not even create the checkbox and I don't know why. Here's my code:

document.write("<form><input type="checkbox"></form>")

Can anyone give me a hint where the problem is?

Bhavik
  • 4,508
  • 3
  • 25
  • 43
MarisP
  • 787
  • 2
  • 9
  • 20
  • 1
    Change text to checkbox and also your quotes are nested.. – Avneesh Raghav Jun 13 '14 at 09:00
  • use this, var x = document.createElement("INPUT"); x.setAttribute("type", "checkbox"); – cracker Jun 13 '14 at 09:03
  • 1
    Not exactly related, but why are you creating this input dynamically? There's nothing dynamic in the output, and this could be replaced with regular inline HTML code. Notice, that you can't use [`document.write()`](https://developer.mozilla.org/en-US/docs/Web/API/document.write) after the page has been parsed, hence your snippet can't be a part of an event handler. – Teemu Jun 13 '14 at 09:05
  • 1
    @Teemu Exactly (as per my comments to answers)! It is related, because using `document.write` is a huge mistake. You either want to manipulate dynamically added element (and thus you use `createElement` to have handle to it) or you use plain HTML. – trejder Jun 13 '14 at 09:29

3 Answers3

4

First of all you are not escaping quotes;

document.write("<form><input type=\"text\"></form>") this will create textbox

try this

document.write('<form><input type="checkbox"></form>');
saygun
  • 1,409
  • 12
  • 24
  • Thank you, this fixed the problem! – MarisP Jun 13 '14 at 09:01
  • 1
    I prefer _Ishan Jain_'s answer much more, because it shows, how to correctly, dynamically create a checkbox (or any other element). `document.write` is a crap, because you don't have handle to created element to do _something_ with it. And you don't generate elements dynamically in Javascript to only put them to the document (you'd use plain HTML in this case). You most likely create them dynamically to do some operations, manipulate them somehow etc. – trejder Jun 13 '14 at 09:27
2

In your code you are not escaping quotes.

You should escape the quotes.

document.write('<form><input type="checkbox"></form>')

But i suggest you to not use document.write(), because it's not a good practice. It replaces entire document.

So, you should try to create element dynamically using javascript.

Try this code:

var checkbox = document.createElement('input');
checkbox.type = "checkbox";
checkbox.name = "name";
checkbox.value = "value";
checkbox.id = "id";
document.body.appendChild(checkbox);
Community
  • 1
  • 1
Ishan Jain
  • 7,501
  • 9
  • 45
  • 72
1

Working Fiddle

var cb = document.createElement('input');
cb.type = "checkbox";
cb.name = "name";
cb.value = "value";
cb.id = "id";
cb.addEventListener("click", showTime, false);

var lbl = document.createElement('label')
lbl.htmlFor = "id";
lbl.appendChild(document.createTextNode("show time"));

var container = document.getElementById('container');
container.appendChild(cb);
container.appendChild(lbl);

var sp = document.getElementById('time');

function showTime() {
    if (this.checked) {
        var today = new Date()
        sp.innerHTML = today.toString();
    } else {
        sp.innerHTML='';
    }
}
Community
  • 1
  • 1
Bhavik
  • 4,508
  • 3
  • 25
  • 43
  • Improve your answering quality, please: (a) don't just put a link and code, put some explanation, why your're proposing this or that solution, (b) paste only _important_ parts of code, the one you're referring to, not the entire code, (c) don't put junk like `Hope it helps..!!`, it is completely useless here. Thanks! – trejder Jun 13 '14 at 09:25