Question:
I am using an array on the client side to create an HTML string that builds a table of data, which the user can then manipulate. Because the array is on the client side and the user can insert, delete, and change data, I’m using a text range on the body and pasteHTML to render the table to the user.
The table and data appear but none of the events tied to the elements are firing. These events are included in the HTML string. I’ve tried encoding quotes (chr(34)) which ended up making a set of double quotes, no quotes, and a single quote. None of these seem to have any effect. What is odd is that I can do a document.write so that I can see the rendered HTML, copy this to a new page, add in the subs, and the events fire. I’ve searched everywhere and can’t find any topics that would indicate what my problem could be.
Answer:
Event handling can get to be ugly when dealing with dynamically generated HTML. The HTML that is created by using strings will set up the elements, but because the event queue is established during the initial creation of the HTML, events that are established through the use of strings will typically not get passed into the event handling mechanism of the page.
That doesn’t mean that you can’t create events on the fly. In order to make this work, though, you may be better off using the object definitions within DHTML, rather than building the tables from strings. These objects are added within the document object model itself, so they give you a finer level of control, and you can explicitly set events on them as a distinct operation rather than letting the parser try to reconstruct the table from a string.
For the sake of discussion, assume that the table will be contained in a separate DIV element with an id of “dataTableDiv”. This code will add a table to this DIV and attach events to it:
The example in the makeTable() function is fairly pedagogical in nature to show how each of the individual components work. Initially, the DIV is emptied using .innerHTML=””. This works best when you’re recreating the table from scratch after some event. One caveat about this method of deleting elements?if you assign an HTML element in the table to a variable referenced indirectly (for example, set myCell=document.all(“ABC”)), erasing the table using innerHTML will destroy the object references, undoubtedly causing code problems down the road.
The function .createElement() is used to create an HTML element. Because of some inner constraints in the object model, the objects being created must be passed as uppercase entities (document.createElement(“TABLE”) rather than document.createElement(“table”)). The function returns the element just created as an argument.
The elements so created do not yet belong to the “rendering tree” of the page?you can assign properties to these elements, but until the element is attached to the tree, it will not render. The function dataTableDiv.appendChild does just that, appending the table element as the last child of the dataTableDiv element. You can also use .insertBefore to place the new element before an already extant element in the rendering tree.
Because Web designers employ tables so often, Microsoft provided some shortcuts for generating tables. The .insertRow function adds a new row (a
Back to the original problem: once you do have a reference to these cell or row objects, you can assign event handlers to them in a similar way to how you assign attributes. The one difference is in the use of getRef(). This function (specific to VBS5) takes the name of a function as an argument, and returns a reference to the function that will be called whenever the event in question takes place. For example, assign the highlightRow function to the onmouseover event, and the restoreRow function to the onmouseout event:
row.onmouseover=getRef("highlightRow") row.onmouseout=getRef("restoreRow")
Taken together, these cause the background of the row to change color when the mouse moves over or out of a given row.
So what about situations where VBScript 5.0 is not available (such as older versions of Internet Explorer 4.0)? To be honest, the best solution there is to make use of JavaScript, which has had this capability from the beginning, rather than Visual Basic Script. This is in fact one of the reasons that most Web developers work with JavaScript on the client. You may also be able to short circuit the getRef function using a JavaScript call as well:
In my example, I’ve hard coded values into the makeTable function, although in practice you will almost certainly want to populate the table’s cells using an array, data-binding, or perhaps XML rather than code the values in explicitly. This is the recommended way of creating DHTML output (at least in IE)?not only does it keep the integrity of the rendering tree intact, but it is often an order of magnitude faster to render than working with a string. Once Netscape 5.0 comes on line with its adoption of the W3C document object model, then it will also be cross-compatible between the generation 5 browsers.


What We Should Expect from Cell Phone Tech in the Near Future
The earliest cell phones included boxy designs full of buttons and antennas, and they only made calls. Needless to say, we’ve come a long way from those classic brick phones


The Best Mechanical Keyboards For Programmers: Where To Find Them
When it comes to programming, a good mechanical keyboard can make all the difference. Naturally, you would want one of the best mechanical keyboards for programmers. But with so many


The Digital Panopticon: Is Big Brother Always Watching Us Online?
In the age of digital transformation, the internet has become a ubiquitous part of our lives. From socializing, shopping, and learning to more sensitive activities such as banking and healthcare,


Embracing Change: How AI Is Revolutionizing the Developer’s Role
The world of software development is changing drastically with the introduction of Artificial Intelligence and Machine Learning technologies. In the past, software developers were in charge of the entire development


The Benefits of Using XDR Solutions
Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved


How AI is Revolutionizing Fraud Detection
Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across


Companies Leading AI Innovation in 2023
Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several


Step-by-Step Guide to Properly Copyright Your Website
Creating a website is not easy, but protecting your website is equally important. Implementing copyright laws ensures that the substance of your website remains secure and sheltered. Copyrighting your website


Fivetran Pricing Explained
One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of


Kubernetes Logging: What You Need to Know
Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes


Why Is Ransomware Such a Major Threat?
One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing


Tools You Need to Make a Data Dictionary
Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as