最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change HTML checkbox checked attribute - Stack Overflow

programmeradmin4浏览0评论

Is it possible to change the "checked" attribute of a checkbox? So I want to change the preselect value in the HTML. (So that I can get it later with innerHTML of JavaScript).

Is it possible to change the "checked" attribute of a checkbox? So I want to change the preselect value in the HTML. (So that I can get it later with innerHTML of JavaScript).

Share Improve this question asked Oct 27, 2011 at 20:38 LaheyLahey 2312 gold badges4 silver badges11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

Sure, if you're going to use innerHTML in Javascript you could just reload the whole checkbox bit and make it checked or not. However the easier way to do it is to just change the checked attribute within Javascript if you're going to use JS anyway.

document.getElementById("myCheckboxId").checked = true;

EDIT

Ok, so you need to be able to get it again with innerHTML in the future. Like I said in my first sentence, you could just use innerHTML to change the checkbox as well. May be a bit hackish but could be fine if you're doing it already.

Here's a test html page with links to check/uncheck via JS, check/uncheck by swapping out the whole checkbox with innerHTML, and to get innerHTML for testing. When you do the innerHTML swap, it actually changes what's returned by innerHTML later. Is this what you mean? Note: hmm I didn't realize it until now but it appears that firefox is changing my checked to checked="checked", interesting. So the view innerHTML link shows me checked="checked" and when unchecked shows nothing. Firebug shows it change also, but slightly differently - going from checked="" to nothing.

Here is jsfiddle!

<span id="testdiv1"><input type="checkbox" name="heyhey" id="heyhey" CHECKED></span>
<p>
    <a href="#" onClick="document.getElementById('heyhey').checked = false;">uncheck via JS</a><br>
    <a href="#" onClick="document.getElementById('heyhey').checked = true;">check via JS</a><br>
    <a href="#" onClick="document.getElementById('testdiv1').innerHTML = '<input type=\'checkbox\' name=\'heyhey\' id=\'heyhey\' >';">uncheck via innerHTML swap</a><br>
    <a href="#" onClick="document.getElementById('testdiv1').innerHTML = '<input type=\'checkbox\' name=\'heyhey\' id=\'heyhey\' checked>';">check via innerHTML swap</a><br>
    <a href="#" onClick="alert('innerHTML!: '+'\n\n'+getElementById('testdiv1').innerHTML)">view innerHTML</a>
</p>

I don't know if I escaped all those properly but works for me. Best to not do it inline.

Sounds like you have a legitimate use for setAttribute() rather than the equivalent property (checked). Assuming you have a checkbox stored in a variable called checkbox, you can use the following to set the checked attribute to be checked by default:

checkbox.setAttribute("checked", "checked");

... and the following to remove the attribute and make the checkbox unchecked by default:

checkbox.removeAttribute("checked");

Note that this won't work properly on older IE, which has broken implementations of attribute-related methods.

I've written about this in more detail in an answer to previous question: Add attribute 'checked' on click jquery

Yes it is possible

checkboxObject.checked=true|false

发布评论

评论列表(0)

  1. 暂无评论