htmlElement.SetAttribute

Hi Experts,

Below you will find my html test page and the vb.Net code I am using to try to turn both the <a> tag backgrounds green. Why does it not work?



<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>H Test</title>
</head>
<body>
    <div >
        <a style="background-color:Red;" href="http://www.google.com">Google</a></div>
    <br />
    <div>
        <a href="http://www.yahoo.com">Yahoo</a></div>
</body>
</html>


Dim elementCollection As HtmlElementCollection

elementCollection = WebBrowser1.Document.GetElementsByTagName("a")

        For Each element As HtmlElement In elementCollection

            element.SetAttribute("style", "background-color:green;")

        Next
DColinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CodeCruiserCommented:
Have you stepped through the code to see if it is running through the for loop?

Does changing green to Green help?
0
DColinAuthor Commented:
The code is running the loop and Green makes no difference.

Does the browser need to be refreshed after the attributes are changed or does the SetAttribute do that automatically?
0
CodeCruiserCommented:
It should do it automatically. Experiment by adding a button which does the same thing in Javascript and see if that works.
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

DColinAuthor Commented:
The following javascript works.

        function TurnGreen() {

            var elementsCol = document.getElementsByTagName("a")

            for (var index = 0; index < elementsCol.length; index++) {
                document.getElementsByTagName("a")[index].setAttribute("style", "background-color:Green");
            }
           
        }
0
DColinAuthor Commented:
Accessing the elements style property like this works

element.Style = "background-color: green"

This for some reason does not.

element.SetAttribute("style", "background-color:green;")
0
ArkCommented:
Accessing the elements style property like this works
You'r correct. HTMLElement methods Get/SetAttribute names are wrong. Actually, they access element's PROPERTY (string OR object) rather then ATTRIBUTE (strings only). You can check this by Debug.Print (element.GetAttribute("style")) - you'll get "com_Object" (== style PROPERTY) instead of semi-colon separated string (==style ATTRIBUTE). Since SetAttribute allows strings only, for complex properties like style in pseudo-code it should be smth like
element.SetAttribute("style.backgroundColor","green") - but in real code this cannot be done.
So you must use element.Style syntax for styles.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.