Add class to an element within an HTML Application from within VBS function

1.9k views Asked by At

I have an HTA that is displayed when a backup routine is complete and I'd like to be able to manipulate the HTA via VBS, showing whether or not a series of folders were backed up correctly.

So, using the ID of an element within the HTA, is it possible to add a class to the element?

For example -

If result = true Then {add class 'success' to id 'result-documents'}

Here is my VBS for showing the HTA -

Shell.Run COMPLETED_MESSAGE_PATH, 0, True

And example of an element that I wish to target is as below. For example, here I'd like to add a class attribute (the class name varies) to the element with ID 'result-documents' -

<span class="list-item"><span id="result-documents"></span>Documents</span>
1

There are 1 answers

4
Ansgar Wiechers On BEST ANSWER

If the element in question doesn't have a class attribute already you need to create one first before you can assign a value to it:

Set p = IE.Document.getElementById("result-documents")

Set attr = IE.Document.createAttribute("class")
attr.value = "success"
p.setAttributeNode(attr)

For running this code directly in an HTA just remove the IE object, because IE already is your runtime environment in that context:

Set p = document.getElementById("result-documents")

Set attr = document.createAttribute("class")
attr.value = "success"
p.setAttributeNode(attr)

If your element ID didn't have a hyphen in it (e.g. resultsDocuments instead of results-documents) you could even use its ID directly:

Set attr = document.createAttribute("class")
attr.value = "success"
resultDocuments.setAttributeNode(attr)