Set textarea value with HtmlAgilityPack

asp.net c# html-agility-pack

Question

I am using HtmlAgilityPack and it seems I cannot set the value of the text in a textarea in the same way as an input field:

var node = doc.DocumentNode.SelectSingleNode("//textarea");
if (node != null)
{
    node.SetAttributeValue("value", record.Data);
}

Does anyone know how this can be done?

Accepted Answer

A <textarea> element doesn't have a value attribute. It's content is it's own text node:

<textarea>
Some content
</textarea>

To access that, use the .InnerHtml property:

var node = doc.DocumentNode.SelectSingleNode("//textarea");
if (node != null)
{
    node.InnerHtml = record.Data;
}



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why