Quaze
Quaze

Reputation: 73

HtmlElement.GetAttribute("for") Returns empty string

I'm playing with the webbrowser control and the htmlElementcollection class to get all html inside a form. i made this function to loop trough everything, and if a label is found it must save the value in the 'For' attribute, and the textvalue of the label. then when an input element is found it must check if it's name contains the text saved in the variable ForID, if so, append and show.

code:

private string logLabelInputs(HtmlElementCollection collection) {
        StringBuilder sb = new StringBuilder();
        var forID = "";
        var labelValue = "";
        foreach (HtmlElement item in collection) {
            if (item.TagName.ToLower() == "label") {
                forID = item.GetAttribute("for");
                forID = forID == "" ? item.GetAttribute("FOR") : forID;
                labelValue = item.InnerText;
            }
            if (item.TagName.ToLower() == "input") {
                if(item.Name.Contains(forID)) {
                    sb.Append(labelValue + ": " + item.Name + "\r\n\r\n");
                }
            }
            if (item.All.Count > 0) {
                sb.Append(logLabelInputs(item.All));
            }               
        }
        return sb.ToString();
    }

private void button4_Click(object sender, EventArgs e) {
        HtmlElementCollection tagsCollection = webBrowser1.Document.GetElementsByTagName("form");
        textBox1.Text = logLabelInputs(tagsCollection);
    }

So what happpens is that forID is always empty. the value is saved correctly. I check for uppercase and lowercase (tough i KNOW it's lowercase)

ANyone have any ideas about this?

Upvotes: 2

Views: 1566

Answers (2)

Yo Momma
Yo Momma

Reputation: 8781

use this:

item.GetAttribute("htmlfor") 

Upvotes: 2

Andrei Andrushkevich
Andrei Andrushkevich

Reputation: 9973

it will be better to use any javascript framework like jQuery, or Prototype for this. this javascript will be cross browser.

Upvotes: 0

Related Questions