Link to home
Start Free TrialLog in
Avatar of DColin
DColinFlag for Thailand

asked on

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
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Does changing green to Green help?
Avatar of DColin

ASKER

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?
It should do it automatically. Experiment by adding a button which does the same thing in Javascript and see if that works.
Avatar of DColin

ASKER

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");
            }
           
        }
Avatar of DColin

ASKER

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;")
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial