Skip to content Skip to sidebar Skip to footer

Javascript Innerhtml Of Checkbox

Well I have some checkboxes with id='0','1','2','3' etc. So I need to get text of checkboxes. Here is code of checkboxes in PHP. 'Copy

Then you could get:

checkbox.parentNode.innerHTML;

or

checkbox.parentNode.getElementsByTagName('span')[0].innerHTML

Solution 2:

An input element does not have an innerhtml because the element is supposed to be self-closing:

<input type="checkbox" name="checkbox" value="1" />

Solution 3:

You can't because <input> doesn't have close tag, so it doesn't have innerHTML property:

Must have a start tag and must not have an end tag.

From MDN.

I recomend you to use combination of label and input.

Solution 4:

Ideally a input element will not have any innerHTML. That means you cannot have an enclosing html content in input element. You can just have value of input element.

Solution 5:

As pointed out, input elements are self-closing and do not have innerHTML. Best solution if you want to catch the plain text next to the checkbox element would be to use "nextElementSibling" and "innerText".

<inputtype="checkbox"id="checkbox1"><span>Text to get...</span><buttononclick="getText()">Get Text</button>

JavaScript:

functiongetText() {
     console.log(document.getElementById("checkbox1").nextElementSibling.innerText);
}

Post a Comment for "Javascript Innerhtml Of Checkbox"