On my last job I did a lot of web pages that required the same coding to be used over and over ad infinitum, and to save my sanity I developed a few JavaScript shortcuts. Now, six years later, I’m in a similar situation, and would like to use the same tricks. The trouble is that I haven’t touched JavaScript in all of that time and don’t have any notes to refresh my memory.
Here’s what I want to do.
I am creating a simple two-column table in a web page. It will have many rows that are identical except for the text. So I need to create a script that defines a table row, allowing for two variables (the content of row A and the content of row B). I then enter a command statement for each row that is to be inserted, defining the variables.
That’s it.
I’ve spent all morning going through online JavaScript tutorials and made some progress, but I’m not a gifted programmer and would prefer to cut to the chase if possible.
I don’t quite understand what you mean by “I then enter a command statement for each row that is to be inserted, defining the variables.”
Are you saying you want to set up a big 2D array, and have a Javascript function build the table code dynamically? Because that’s pretty easy, but in general a terrible idea. This is a job for a server-side templating system, even if it’s one that just generates static files.
Sorry if I wasn’t clear, and I’m not asking for the most efficient tool possible. I’m writing up a web page that is little more than a two column table. Each cell in column A contains a term to be defined. The corresponding cell in column B contains the definition.
I’ve done this a hundred times already in basic, mind-numbingly boring HTML coding (thank Og for copy and paste). This particular table, though, has so many rows that it is maxing out the character limit of the database where it is posted.
With the research I’ve done this morning, I’ve put together a script that comes close to what I need. I hope it won’t be a violation of policies to post this, but I’m putting it in comment tags as well as quotes as a precaution:
I’ve tried to add new rows to the table by repeating the “dict=new…” command with new argument values. Instead of creating a new table row, however, I get a single row with both sets of values together.
Well, there’s a few things wrong here. There’s no need to use an object constructor if you just want to append some HTML with document.write. You also really, really should not be using <font> tags to control the style; you should attach some CSS classes to your table instead.
But this raises the question: if you’re going to all this trouble to write some Javascript code to convert this data into a table in the browser, why not instead spend your time writing a program to do that once and spit out the HTML you need, and then paste it into your HTML file? This saves you from having to test it on multiple browsers, and makes the table data visible to web spiders and other programs.
But if you must do it in Javascript, you can use something as simple as this: