Skip to content Skip to sidebar Skip to footer

Access Tag "option" In HtmlElement

need to change this: I

Solution 1:

Once you have the element, you can loop through the children and update the selected attribute:

var ele = webBrowser1.Document.GetElementById("asdf");

if (ele != null)
{
    foreach (HtmlElement child in ele.Children)
    {
        child.SetAttribute("selected", "false");
        if (child.InnerText == "c")
            child.SetAttribute("selected", "true");
    }
}

Solution 2:

This is done easily with

htmlEle.value = "c";

Live DEMO


Solution 3:

Assuming: htmlEle is the option Element,

C#: Please try:

  htmlEle.textContent = "a1";

to make the option appearing gas selected,

 htmlEle.setAttribute("selected", "true");

HTML/JavaScript:

You mean you want to change the display value of first option from a to c, then try below:

      htmlEle.innerHTML = "c";

to make the option appearing gas selected,

     htmlEle.setAttribute("selected", "selected");

If I have an ID assigned to selectbox as:

      <select name="asdf" id="selectBox">
         <option selected>a</option>
         <option>b</option>
         <option>c</option>
       </select>

Then

  var selectElem = document.getElementById("selectBox");
  selectElem.childNodes[1].innerHTML = "a1";

changes value of first option as a1.


Post a Comment for "Access Tag "option" In HtmlElement"