DColin
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:Re d;" 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.GetEl ementsByTa gName("a")
For Each element As HtmlElement In elementCollection
element.SetAttribute("styl e", "background-color:green;")
Next
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:Re
<br />
<div>
<a href="http://www.yahoo.com">Yahoo</a></div>
</body>
</html>
Dim elementCollection As HtmlElementCollection
elementCollection = WebBrowser1.Document.GetEl
For Each element As HtmlElement In elementCollection
element.SetAttribute("styl
Next
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?
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.
ASKER
The following javascript works.
function TurnGreen() {
var elementsCol = document.getElementsByTagN ame("a")
for (var index = 0; index < elementsCol.length; index++) {
document.getElementsByTagN ame("a")[i ndex].setA ttribute(" style", "background-color:Green");
}
}
function TurnGreen() {
var elementsCol = document.getElementsByTagN
for (var index = 0; index < elementsCol.length; index++) {
document.getElementsByTagN
}
}
ASKER
Accessing the elements style property like this works
element.Style = "background-color: green"
This for some reason does not.
element.SetAttribute("styl e", "background-color:green;")
element.Style = "background-color: green"
This for some reason does not.
element.SetAttribute("styl
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Does changing green to Green help?