Om Sao
Om Sao

Reputation: 7683

How to get the text part of this DOM?

I have in my this.nameHtml a DOM object with value:

<span class='test-name'>333</span>

I want to access the innerHTML i.e. 333

basically I am creating a checkbox element and want to assign the 333 value from DOM object in name attribute as follow:

checkBox = document.createElement("input");
checkBox.type = "checkbox";
checkBox.name = this.nameHtml;   // Here i want to store 333 
checkBox.className = "TriageCheckBox";

Please suggest a way.

Upvotes: 0

Views: 55

Answers (2)

Benedikt
Benedikt

Reputation: 974

checkBox.name = this.nameHtml.innerText;

Beware of innerHtml! This may print unwanted html code and destroy your site structure! Difference between innerText and innerHTML in javascript

Regards, Benedikt

Upvotes: 0

Hotted24
Hotted24

Reputation: 512

Try using innerHTML property :

checkBox.name = document.getElementByClassName("test-name")[0].innerHTML;

NB : document.getElementByClassName("test-name")[0] get first element with test-name class, be sure to adjust index if multiple ones.

Upvotes: 1

Related Questions