Reputation: 7514
Shouldn't this javascript just display an unchecked checkbox followed by the text "MyCheckBox"? Instead, it's just displaying "checked."
<html>
<head>
<script>
var data = false;
document.write('<input type="checkbox" ' + data ? "checked" : "" + '>MyCheckBox');
</script>
</head>
<body>
</body>
</html>
Upvotes: 0
Views: 88
Reputation: 1066
Even when you can have the desired result with this line:
document.write("<input type='checkbox' " + (data ? "checked" : "") + ">MyCheckBox");
It is not the best way, you are writing to a stream and that is a risky business. Handling in the structured way, you can do it in this way:
`var data = false;
var check=document.createElement("input");
check.id="mycheckbox"
check.type="checkbox";
check.checked=data;
document.body.appendChild(check)`
Upvotes: 0
Reputation: 1
Just Do This.
var data=false,
a=document.createElement("input");
a.setAttribute("type","checkbox");
a.checked=data;
document.body.append(a);
Upvotes: 0
Reputation: 979
Here's the correction:
document.write('<input type="checkbox" ' + (data ? "checked" : "") + '>MyCheckBox');
You were missing the parentheses.
Also, don't put that inside <head>
, like Teemu said, you can't render HTML there.
Upvotes: 1